bootstrap.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. <?php
  2. namespace Symfony\Component\DependencyInjection
  3. {
  4. interface ContainerInterface
  5. {
  6. const EXCEPTION_ON_INVALID_REFERENCE = 1;
  7. const NULL_ON_INVALID_REFERENCE = 2;
  8. const IGNORE_ON_INVALID_REFERENCE = 3;
  9. const SCOPE_CONTAINER = 'container';
  10. const SCOPE_PROTOTYPE = 'prototype';
  11. function set($id, $service, $scope = self::SCOPE_CONTAINER);
  12. function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);
  13. function has($id);
  14. function getParameter($name);
  15. function hasParameter($name);
  16. function setParameter($name, $value);
  17. function enterScope($name);
  18. function leaveScope($name);
  19. function addScope($name, $parentScope = self::SCOPE_CONTAINER);
  20. function hasScope($name);
  21. function isScopeActive($name);
  22. }
  23. }
  24. namespace Symfony\Component\DependencyInjection
  25. {
  26. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  27. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  28. use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
  29. class Container implements ContainerInterface
  30. {
  31. protected $parameterBag;
  32. protected $services;
  33. protected $scopes;
  34. protected $scopeChildren;
  35. protected $scopedServices;
  36. protected $scopeStacks;
  37. protected $loading = array();
  38. public function __construct(ParameterBagInterface $parameterBag = null)
  39. {
  40. $this->parameterBag = null === $parameterBag ? new ParameterBag() : $parameterBag;
  41. $this->services =
  42. $this->scopes =
  43. $this->scopeChildren =
  44. $this->scopedServices =
  45. $this->scopeStacks = array();
  46. $this->set('service_container', $this);
  47. }
  48. public function compile()
  49. {
  50. $this->parameterBag->resolve();
  51. $this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
  52. }
  53. public function isFrozen()
  54. {
  55. return $this->parameterBag instanceof FrozenParameterBag;
  56. }
  57. public function getParameterBag()
  58. {
  59. return $this->parameterBag;
  60. }
  61. public function getParameter($name)
  62. {
  63. return $this->parameterBag->get($name);
  64. }
  65. public function hasParameter($name)
  66. {
  67. return $this->parameterBag->has($name);
  68. }
  69. public function setParameter($name, $value)
  70. {
  71. $this->parameterBag->set($name, $value);
  72. }
  73. public function set($id, $service, $scope = self::SCOPE_CONTAINER)
  74. {
  75. if (self::SCOPE_PROTOTYPE === $scope) {
  76. throw new \InvalidArgumentException('You cannot set services of scope "prototype".');
  77. }
  78. $id = strtolower($id);
  79. if (self::SCOPE_CONTAINER !== $scope) {
  80. if (!isset($this->scopedServices[$scope])) {
  81. throw new \RuntimeException('You cannot set services of inactive scopes.');
  82. }
  83. $this->scopedServices[$scope][$id] = $service;
  84. }
  85. $this->services[$id] = $service;
  86. }
  87. public function has($id)
  88. {
  89. $id = strtolower($id);
  90. return isset($this->services[$id]) || method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service');
  91. }
  92. public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
  93. {
  94. $id = strtolower($id);
  95. if (isset($this->services[$id])) {
  96. return $this->services[$id];
  97. }
  98. if (isset($this->loading[$id])) {
  99. throw new \LogicException(sprintf('Circular reference detected for service "%s" (services currently loading: %s).', $id, implode(', ', array_keys($this->loading))));
  100. }
  101. if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')) {
  102. $this->loading[$id] = true;
  103. $service = $this->$method();
  104. unset($this->loading[$id]);
  105. return $service;
  106. }
  107. if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
  108. throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $id));
  109. }
  110. }
  111. public function getServiceIds()
  112. {
  113. $ids = array();
  114. $r = new \ReflectionClass($this);
  115. foreach ($r->getMethods() as $method) {
  116. if (preg_match('/^get(.+)Service$/', $method->getName(), $match)) {
  117. $ids[] = self::underscore($match[1]);
  118. }
  119. }
  120. return array_merge($ids, array_keys($this->services));
  121. }
  122. public function enterScope($name)
  123. {
  124. if (!isset($this->scopes[$name])) {
  125. throw new \InvalidArgumentException(sprintf('The scope "%s" does not exist.', $name));
  126. }
  127. if (self::SCOPE_CONTAINER !== $this->scopes[$name] && !isset($this->scopedServices[$this->scopes[$name]])) {
  128. throw new \RuntimeException(sprintf('The parent scope "%s" must be active when entering this scope.', $this->scopes[$name]));
  129. }
  130. if (isset($this->scopedServices[$name])) {
  131. $services = array($this->services, $name => $this->scopedServices[$name]);
  132. unset($this->scopedServices[$name]);
  133. foreach ($this->scopeChildren[$name] as $child) {
  134. $services[$child] = $this->scopedServices[$child];
  135. unset($this->scopedServices[$child]);
  136. }
  137. $this->services = call_user_func_array('array_diff_key', $services);
  138. array_shift($services);
  139. if (!isset($this->scopeStacks[$name])) {
  140. $this->scopeStacks[$name] = new \SplStack();
  141. }
  142. $this->scopeStacks[$name]->push($services);
  143. }
  144. $this->scopedServices[$name] = array();
  145. }
  146. public function leaveScope($name)
  147. {
  148. if (!isset($this->scopedServices[$name])) {
  149. throw new \InvalidArgumentException(sprintf('The scope "%s" is not active.', $name));
  150. }
  151. $services = array($this->services, $this->scopedServices[$name]);
  152. unset($this->scopedServices[$name]);
  153. foreach ($this->scopeChildren[$name] as $child) {
  154. if (!isset($this->scopedServices[$child])) {
  155. continue;
  156. }
  157. $services[] = $this->scopedServices[$child];
  158. unset($this->scopedServices[$child]);
  159. }
  160. $this->services = call_user_func_array('array_diff_key', $services);
  161. if (isset($this->scopeStacks[$name]) && count($this->scopeStacks[$name]) > 0) {
  162. $services = $this->scopeStacks[$name]->pop();
  163. $this->scopedServices += $services;
  164. array_unshift($services, $this->services);
  165. $this->services = call_user_func_array('array_merge', $services);
  166. }
  167. }
  168. public function addScope($name, $parentScope = self::SCOPE_CONTAINER)
  169. {
  170. if (self::SCOPE_CONTAINER === $name || self::SCOPE_PROTOTYPE === $name) {
  171. throw new \InvalidArgumentException(sprintf('The scope "%s" is reserved.', $name));
  172. }
  173. if (isset($this->scopes[$name])) {
  174. throw new \InvalidArgumentException(sprintf('A scope with name "%s" already exists.', $name));
  175. }
  176. if (self::SCOPE_CONTAINER !== $parentScope && !isset($this->scopes[$parentScope])) {
  177. throw new \InvalidArgumentException(sprintf('The parent scope "%s" does not exist, or is invalid.', $parentScope));
  178. }
  179. $this->scopes[$name] = $parentScope;
  180. $this->scopeChildren[$name] = array();
  181. while ($parentScope !== self::SCOPE_CONTAINER) {
  182. $this->scopeChildren[$parentScope][] = $name;
  183. $parentScope = $this->scopes[$parentScope];
  184. }
  185. }
  186. public function hasScope($name)
  187. {
  188. return isset($this->scopes[$name]);
  189. }
  190. public function isScopeActive($name)
  191. {
  192. return isset($this->scopedServices[$name]);
  193. }
  194. static public function camelize($id)
  195. {
  196. return preg_replace(array('/(?:^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\1')", "'_'.strtoupper('\\1')"), $id);
  197. }
  198. static public function underscore($id)
  199. {
  200. return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($id, '_', '.')));
  201. }
  202. }
  203. }
  204. namespace Symfony\Component\DependencyInjection
  205. {
  206. interface ContainerAwareInterface
  207. {
  208. function setContainer(ContainerInterface $container = null);
  209. }
  210. }
  211. namespace Symfony\Component\DependencyInjection
  212. {
  213. class ContainerAware implements ContainerAwareInterface
  214. {
  215. protected $container;
  216. public function setContainer(ContainerInterface $container = null)
  217. {
  218. $this->container = $container;
  219. }
  220. }
  221. }
  222. namespace Symfony\Component\HttpKernel\Bundle
  223. {
  224. interface BundleInterface
  225. {
  226. function boot();
  227. function shutdown();
  228. function getParent();
  229. function getName();
  230. function getNamespace();
  231. function getPath();
  232. }
  233. }
  234. namespace Symfony\Component\HttpKernel\Bundle
  235. {
  236. use Symfony\Component\DependencyInjection\ContainerAware;
  237. use Symfony\Component\DependencyInjection\ContainerBuilder;
  238. use Symfony\Component\Console\Application;
  239. use Symfony\Component\Finder\Finder;
  240. abstract class Bundle extends ContainerAware implements BundleInterface
  241. {
  242. protected $name;
  243. protected $reflected;
  244. public function boot()
  245. {
  246. }
  247. public function shutdown()
  248. {
  249. }
  250. public function getNamespace()
  251. {
  252. if (null === $this->reflected) {
  253. $this->reflected = new \ReflectionObject($this);
  254. }
  255. return $this->reflected->getNamespaceName();
  256. }
  257. public function getPath()
  258. {
  259. if (null === $this->reflected) {
  260. $this->reflected = new \ReflectionObject($this);
  261. }
  262. return strtr(dirname($this->reflected->getFileName()), '\\', '/');
  263. }
  264. public function getParent()
  265. {
  266. return null;
  267. }
  268. final public function getName()
  269. {
  270. if (null !== $this->name) {
  271. return $this->name;
  272. }
  273. $name = get_class($this);
  274. $pos = strrpos($name, '\\');
  275. return $this->name = false === $pos ? $name : substr($name, $pos + 1);
  276. }
  277. public function registerExtensions(ContainerBuilder $container)
  278. {
  279. if (!$dir = realpath($this->getPath().'/DependencyInjection')) {
  280. return;
  281. }
  282. $finder = new Finder();
  283. $finder->files()->name('*Extension.php')->in($dir);
  284. $prefix = $this->getNamespace().'\\DependencyInjection';
  285. foreach ($finder as $file) {
  286. $class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php');
  287. $container->registerExtension(new $class());
  288. }
  289. }
  290. public function registerCommands(Application $application)
  291. {
  292. if (!$dir = realpath($this->getPath().'/Command')) {
  293. return;
  294. }
  295. $finder = new Finder();
  296. $finder->files()->name('*Command.php')->in($dir);
  297. $prefix = $this->getNamespace().'\\Command';
  298. foreach ($finder as $file) {
  299. $r = new \ReflectionClass($prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php'));
  300. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) {
  301. $application->add($r->newInstance());
  302. }
  303. }
  304. }
  305. }
  306. }
  307. namespace Symfony\Component\HttpKernel\Debug
  308. {
  309. class ErrorHandler
  310. {
  311. protected $levels = array(
  312. E_WARNING => 'Warning',
  313. E_NOTICE => 'Notice',
  314. E_USER_ERROR => 'User Error',
  315. E_USER_WARNING => 'User Warning',
  316. E_USER_NOTICE => 'User Notice',
  317. E_STRICT => 'Runtime Notice',
  318. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  319. );
  320. protected $level;
  321. public function __construct($level = null)
  322. {
  323. $this->level = null === $level ? error_reporting() : $level;
  324. }
  325. public function register()
  326. {
  327. set_error_handler(array($this, 'handle'));
  328. }
  329. public function handle($level, $message, $file, $line, $context)
  330. {
  331. if (0 === $this->level) {
  332. return false;
  333. }
  334. if (error_reporting() & $level && $this->level & $level) {
  335. throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line));
  336. }
  337. return false;
  338. }
  339. }
  340. }
  341. namespace Symfony\Component\HttpKernel
  342. {
  343. use Symfony\Component\HttpFoundation\Request;
  344. interface HttpKernelInterface
  345. {
  346. const MASTER_REQUEST = 1;
  347. const SUB_REQUEST = 2;
  348. function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true);
  349. }
  350. }
  351. namespace Symfony\Component\HttpKernel
  352. {
  353. use Symfony\Component\EventDispatcher\Event;
  354. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  355. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  356. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  357. use Symfony\Component\HttpFoundation\Request;
  358. use Symfony\Component\HttpFoundation\Response;
  359. class HttpKernel implements HttpKernelInterface
  360. {
  361. protected $dispatcher;
  362. protected $resolver;
  363. public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver)
  364. {
  365. $this->dispatcher = $dispatcher;
  366. $this->resolver = $resolver;
  367. }
  368. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  369. {
  370. try {
  371. $response = $this->handleRaw($request, $type);
  372. } catch (\Exception $e) {
  373. if (false === $catch) {
  374. throw $e;
  375. }
  376. $event = new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e));
  377. $response = $this->dispatcher->notifyUntil($event);
  378. if (!$event->isProcessed()) {
  379. throw $e;
  380. }
  381. $response = $this->filterResponse($response, $request, 'A "core.exception" listener returned a non response object.', $type);
  382. }
  383. return $response;
  384. }
  385. protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  386. {
  387. $event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
  388. $response = $this->dispatcher->notifyUntil($event);
  389. if ($event->isProcessed()) {
  390. return $this->filterResponse($response, $request, 'A "core.request" listener returned a non response object.', $type);
  391. }
  392. if (false === $controller = $this->resolver->getController($request)) {
  393. throw new NotFoundHttpException(sprintf('Unable to find the controller for "%s", check your route configuration.', $request->getPathInfo()));
  394. }
  395. $event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request));
  396. $controller = $this->dispatcher->filter($event, $controller);
  397. if (!is_callable($controller)) {
  398. throw new \LogicException(sprintf('The controller must be a callable (%s given).', $this->varToString($controller)));
  399. }
  400. $arguments = $this->resolver->getArguments($request, $controller);
  401. $response = call_user_func_array($controller, $arguments);
  402. if (!$response instanceof Response) {
  403. $event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request));
  404. $response = $this->dispatcher->filter($event, $response);
  405. }
  406. return $this->filterResponse($response, $request, sprintf('The controller must return a response (%s given).', $this->varToString($response)), $type);
  407. }
  408. protected function filterResponse($response, $request, $message, $type)
  409. {
  410. if (!$response instanceof Response) {
  411. throw new \RuntimeException($message);
  412. }
  413. $response = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
  414. if (!$response instanceof Response) {
  415. throw new \RuntimeException('A "core.response" listener returned a non response object.');
  416. }
  417. return $response;
  418. }
  419. protected function varToString($var)
  420. {
  421. if (is_object($var)) {
  422. return sprintf('[object](%s)', get_class($var));
  423. }
  424. if (is_array($var)) {
  425. $a = array();
  426. foreach ($var as $k => $v) {
  427. $a[] = sprintf('%s => %s', $k, $this->varToString($v));
  428. }
  429. return sprintf("[array](%s)", implode(', ', $a));
  430. }
  431. if (is_resource($var)) {
  432. return '[resource]';
  433. }
  434. return str_replace("\n", '', var_export((string) $var, true));
  435. }
  436. }
  437. }
  438. namespace Symfony\Component\HttpKernel
  439. {
  440. use Symfony\Component\DependencyInjection\ContainerInterface;
  441. use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
  442. use Symfony\Component\HttpKernel\HttpKernelInterface;
  443. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  444. interface KernelInterface extends HttpKernelInterface, \Serializable
  445. {
  446. function registerRootDir();
  447. function registerBundles();
  448. function registerContainerConfiguration(LoaderInterface $loader);
  449. function boot();
  450. function shutdown();
  451. function getBundles();
  452. function isClassInActiveBundle($class);
  453. function getBundle($name, $first = true);
  454. function locateResource($name, $dir = null, $first = true);
  455. function getName();
  456. function getEnvironment();
  457. function isDebug();
  458. function getRootDir();
  459. function getContainer();
  460. function getStartTime();
  461. function getCacheDir();
  462. function getLogDir();
  463. }
  464. }
  465. namespace Symfony\Component\HttpKernel
  466. {
  467. use Symfony\Component\DependencyInjection\ContainerInterface;
  468. use Symfony\Component\DependencyInjection\ContainerBuilder;
  469. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  470. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  471. use Symfony\Component\DependencyInjection\Loader\DelegatingLoader;
  472. use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
  473. use Symfony\Component\DependencyInjection\Loader\LoaderResolver;
  474. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  475. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  476. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  477. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  478. use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
  479. use Symfony\Component\HttpFoundation\Request;
  480. use Symfony\Component\HttpKernel\HttpKernelInterface;
  481. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  482. use Symfony\Component\HttpKernel\DependencyInjection\Loader\FileLocator;
  483. abstract class Kernel implements KernelInterface
  484. {
  485. protected $bundles;
  486. protected $bundleMap;
  487. protected $container;
  488. protected $rootDir;
  489. protected $environment;
  490. protected $debug;
  491. protected $booted;
  492. protected $name;
  493. protected $startTime;
  494. const VERSION = '2.0.0-DEV';
  495. public function __construct($environment, $debug)
  496. {
  497. $this->environment = $environment;
  498. $this->debug = (Boolean) $debug;
  499. $this->booted = false;
  500. $this->rootDir = realpath($this->registerRootDir());
  501. $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
  502. if ($this->debug) {
  503. ini_set('display_errors', 1);
  504. error_reporting(-1);
  505. $this->startTime = microtime(true);
  506. } else {
  507. ini_set('display_errors', 0);
  508. }
  509. }
  510. public function __clone()
  511. {
  512. if ($this->debug) {
  513. $this->startTime = microtime(true);
  514. }
  515. $this->booted = false;
  516. $this->container = null;
  517. }
  518. public function boot()
  519. {
  520. if (true === $this->booted) {
  521. return;
  522. }
  523. $this->initializeBundles();
  524. $this->initializeContainer();
  525. foreach ($this->getBundles() as $bundle) {
  526. $bundle->setContainer($this->container);
  527. $bundle->boot();
  528. }
  529. $this->booted = true;
  530. }
  531. public function shutdown()
  532. {
  533. $this->booted = false;
  534. foreach ($this->getBundles() as $bundle) {
  535. $bundle->shutdown();
  536. $bundle->setContainer(null);
  537. }
  538. $this->container = null;
  539. }
  540. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  541. {
  542. if (false === $this->booted) {
  543. $this->boot();
  544. }
  545. return $this->getHttpKernel()->handle($request, $type, $catch);
  546. }
  547. protected function getHttpKernel()
  548. {
  549. return $this->container->get('http_kernel');
  550. }
  551. public function getBundles()
  552. {
  553. return $this->bundles;
  554. }
  555. public function isClassInActiveBundle($class)
  556. {
  557. foreach ($this->getBundles() as $bundle) {
  558. if (0 === strpos($class, $bundle->getNamespace())) {
  559. return true;
  560. }
  561. }
  562. return false;
  563. }
  564. public function getBundle($name, $first = true)
  565. {
  566. if (!isset($this->bundleMap[$name])) {
  567. throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled.', $name));
  568. }
  569. if (true === $first) {
  570. return $this->bundleMap[$name][0];
  571. } elseif (false === $first) {
  572. return $this->bundleMap[$name];
  573. }
  574. }
  575. public function locateResource($name, $dir = null, $first = true)
  576. {
  577. if ('@' !== $name[0]) {
  578. throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
  579. }
  580. if (false !== strpos($name, '..')) {
  581. throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name));
  582. }
  583. $name = substr($name, 1);
  584. list($bundle, $path) = explode('/', $name, 2);
  585. $isResource = 0 === strpos($path, 'Resources');
  586. $files = array();
  587. if (true === $isResource && null !== $dir && file_exists($file = $dir.'/'.$bundle.'/'.substr($path, 10))) {
  588. if ($first) {
  589. return $file;
  590. }
  591. $files[] = $file;
  592. }
  593. foreach ($this->getBundle($bundle, false) as $bundle) {
  594. if (file_exists($file = $bundle->getPath().'/'.$path)) {
  595. if ($first) {
  596. return $file;
  597. }
  598. $files[] = $file;
  599. }
  600. }
  601. if ($files) {
  602. return $files;
  603. }
  604. throw new \InvalidArgumentException(sprintf('Unable to find file "@%s".', $name));
  605. }
  606. public function getName()
  607. {
  608. return $this->name;
  609. }
  610. public function getEnvironment()
  611. {
  612. return $this->environment;
  613. }
  614. public function isDebug()
  615. {
  616. return $this->debug;
  617. }
  618. public function getRootDir()
  619. {
  620. return $this->rootDir;
  621. }
  622. public function getContainer()
  623. {
  624. return $this->container;
  625. }
  626. public function getStartTime()
  627. {
  628. return $this->debug ? $this->startTime : -INF;
  629. }
  630. public function getCacheDir()
  631. {
  632. return $this->rootDir.'/cache/'.$this->environment;
  633. }
  634. public function getLogDir()
  635. {
  636. return $this->rootDir.'/logs';
  637. }
  638. protected function initializeBundles()
  639. {
  640. $this->bundles = array();
  641. $topMostBundles = array();
  642. $directChildren = array();
  643. foreach ($this->registerBundles() as $bundle) {
  644. $name = $bundle->getName();
  645. if (isset($this->bundles[$name])) {
  646. throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"', $name));
  647. }
  648. $this->bundles[$name] = $bundle;
  649. if ($parentName = $bundle->getParent()) {
  650. if (isset($directChildren[$parentName])) {
  651. throw new \LogicException(sprintf('Bundle "%s" is directly extended by two bundles "%s" and "%s".', $parentName, $name, $directChildren[$parentName]));
  652. }
  653. $directChildren[$parentName] = $name;
  654. } else {
  655. $topMostBundles[$name] = $bundle;
  656. }
  657. }
  658. if (count($diff = array_values(array_diff(array_keys($directChildren), array_keys($this->bundles))))) {
  659. throw new \LogicException(sprintf('Bundle "%s" extends bundle "%s", which is not registered.', $directChildren[$diff[0]], $diff[0]));
  660. }
  661. $this->bundleMap = array();
  662. foreach ($topMostBundles as $name => $bundle) {
  663. $bundleMap = array($bundle);
  664. $hierarchy = array($name);
  665. while (isset($directChildren[$name])) {
  666. $name = $directChildren[$name];
  667. array_unshift($bundleMap, $this->bundles[$name]);
  668. $hierarchy[] = $name;
  669. }
  670. foreach ($hierarchy as $bundle) {
  671. $this->bundleMap[$bundle] = $bundleMap;
  672. array_pop($bundleMap);
  673. }
  674. }
  675. }
  676. protected function initializeContainer()
  677. {
  678. $class = $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
  679. $location = $this->getCacheDir().'/'.$class;
  680. $reload = $this->debug ? $this->needsReload($class, $location) : false;
  681. $fresh = false;
  682. if ($reload || !file_exists($location.'.php')) {
  683. $container = $this->buildContainer();
  684. $this->dumpContainer($container, $class, $location.'.php');
  685. $fresh = true;
  686. }
  687. require_once $location.'.php';
  688. $this->container = new $class();
  689. $this->container->set('kernel', $this);
  690. if ($fresh && 'cli' !== php_sapi_name()) {
  691. $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
  692. }
  693. }
  694. protected function getKernelParameters()
  695. {
  696. $bundles = array();
  697. foreach ($this->bundles as $name => $bundle) {
  698. $bundles[$name] = get_class($bundle);
  699. }
  700. return array_merge(
  701. array(
  702. 'kernel.root_dir' => $this->rootDir,
  703. 'kernel.environment' => $this->environment,
  704. 'kernel.debug' => $this->debug,
  705. 'kernel.name' => $this->name,
  706. 'kernel.cache_dir' => $this->getCacheDir(),
  707. 'kernel.logs_dir' => $this->getLogDir(),
  708. 'kernel.bundles' => $bundles,
  709. 'kernel.charset' => 'UTF-8',
  710. ),
  711. $this->getEnvParameters()
  712. );
  713. }
  714. protected function getEnvParameters()
  715. {
  716. $parameters = array();
  717. foreach ($_SERVER as $key => $value) {
  718. if ('SYMFONY__' === substr($key, 0, 9)) {
  719. $parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
  720. }
  721. }
  722. return $parameters;
  723. }
  724. protected function needsReload($class, $location)
  725. {
  726. if (!file_exists($location.'.meta') || !file_exists($location.'.php')) {
  727. return true;
  728. }
  729. $meta = unserialize(file_get_contents($location.'.meta'));
  730. $time = filemtime($location.'.php');
  731. foreach ($meta as $resource) {
  732. if (!$resource->isUptodate($time)) {
  733. return true;
  734. }
  735. }
  736. return false;
  737. }
  738. protected function buildContainer()
  739. {
  740. $parameterBag = new ParameterBag($this->getKernelParameters());
  741. $container = new ContainerBuilder($parameterBag);
  742. foreach ($this->bundles as $bundle) {
  743. $bundle->registerExtensions($container);
  744. if ($this->debug) {
  745. $container->addObjectResource($bundle);
  746. }
  747. }
  748. if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) {
  749. $container->merge($cont);
  750. }
  751. $container->compile();
  752. return $container;
  753. }
  754. protected function dumpContainer(ContainerBuilder $container, $class, $file)
  755. {
  756. foreach (array('cache', 'logs') as $name) {
  757. $dir = $container->getParameter(sprintf('kernel.%s_dir', $name));
  758. if (!is_dir($dir)) {
  759. if (false === @mkdir($dir, 0777, true)) {
  760. die(sprintf('Unable to create the %s directory (%s)', $name, dirname($dir)));
  761. }
  762. } elseif (!is_writable($dir)) {
  763. die(sprintf('Unable to write in the %s directory (%s)', $name, $dir));
  764. }
  765. }
  766. $dumper = new PhpDumper($container);
  767. $content = $dumper->dump(array('class' => $class));
  768. if (!$this->debug) {
  769. $content = self::stripComments($content);
  770. }
  771. $this->writeCacheFile($file, $content);
  772. if ($this->debug) {
  773. $container->addObjectResource($this);
  774. $this->writeCacheFile($this->getCacheDir().'/'.$class.'.meta', serialize($container->getResources()));
  775. }
  776. }
  777. protected function getContainerLoader(ContainerInterface $container)
  778. {
  779. $resolver = new LoaderResolver(array(
  780. new XmlFileLoader($container, new FileLocator($this)),
  781. new YamlFileLoader($container, new FileLocator($this)),
  782. new IniFileLoader($container, new FileLocator($this)),
  783. new PhpFileLoader($container, new FileLocator($this)),
  784. new ClosureLoader($container, new FileLocator($this)),
  785. ));
  786. return new DelegatingLoader($resolver);
  787. }
  788. static public function stripComments($source)
  789. {
  790. if (!function_exists('token_get_all')) {
  791. return $source;
  792. }
  793. $output = '';
  794. foreach (token_get_all($source) as $token) {
  795. if (is_string($token)) {
  796. $output .= $token;
  797. } elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
  798. $output .= $token[1];
  799. }
  800. }
  801. $output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
  802. return $output;
  803. }
  804. protected function writeCacheFile($file, $content)
  805. {
  806. $tmpFile = tempnam(dirname($file), basename($file));
  807. if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
  808. chmod($file, 0644);
  809. return;
  810. }
  811. throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
  812. }
  813. public function serialize()
  814. {
  815. return serialize(array($this->environment, $this->debug));
  816. }
  817. public function unserialize($data)
  818. {
  819. list($environment, $debug) = unserialize($data);
  820. $this->__construct($environment, $debug);
  821. }
  822. }
  823. }
  824. namespace Symfony\Component\HttpFoundation
  825. {
  826. class ParameterBag
  827. {
  828. protected $parameters;
  829. public function __construct(array $parameters = array())
  830. {
  831. $this->parameters = $parameters;
  832. }
  833. public function all()
  834. {
  835. return $this->parameters;
  836. }
  837. public function keys()
  838. {
  839. return array_keys($this->parameters);
  840. }
  841. public function replace(array $parameters = array())
  842. {
  843. $this->parameters = $parameters;
  844. }
  845. public function add(array $parameters = array())
  846. {
  847. $this->parameters = array_replace($this->parameters, $parameters);
  848. }
  849. public function get($key, $default = null)
  850. {
  851. return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
  852. }
  853. public function set($key, $value)
  854. {
  855. $this->parameters[$key] = $value;
  856. }
  857. public function has($key)
  858. {
  859. return array_key_exists($key, $this->parameters);
  860. }
  861. public function remove($key)
  862. {
  863. unset($this->parameters[$key]);
  864. }
  865. public function getAlpha($key, $default = '')
  866. {
  867. return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
  868. }
  869. public function getAlnum($key, $default = '')
  870. {
  871. return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
  872. }
  873. public function getDigits($key, $default = '')
  874. {
  875. return preg_replace('/[^[:digit:]]/', '', $this->get($key, $default));
  876. }
  877. public function getInt($key, $default = 0)
  878. {
  879. return (int) $this->get($key, $default);
  880. }
  881. }
  882. }
  883. namespace Symfony\Component\HttpFoundation
  884. {
  885. use Symfony\Component\HttpFoundation\File\UploadedFile;
  886. class FileBag extends ParameterBag
  887. {
  888. private $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
  889. public function __construct(array $parameters = array())
  890. {
  891. parent::__construct();
  892. $this->replace($parameters);
  893. }
  894. public function replace(array $files = array())
  895. {
  896. $this->parameters = array();
  897. $this->add($files);
  898. }
  899. public function set($key, $value)
  900. {
  901. if (is_array($value) || $value instanceof UploadedFile) {
  902. parent::set($key, $this->convertFileInformation($value));
  903. }
  904. }
  905. public function add(array $files = array())
  906. {
  907. foreach ($files as $key => $file) {
  908. $this->set($key, $file);
  909. }
  910. }
  911. protected function convertFileInformation($file)
  912. {
  913. if ($file instanceof UploadedFile) {
  914. return $file;
  915. }
  916. $file = $this->fixPhpFilesArray($file);
  917. if (is_array($file)) {
  918. $keys = array_keys($file);
  919. sort($keys);
  920. if ($keys == $this->fileKeys) {
  921. $file['error'] = (int) $file['error'];
  922. }
  923. if ($keys != $this->fileKeys) {
  924. $file = array_map(array($this, 'convertFileInformation'), $file);
  925. } else
  926. if ($file['error'] === UPLOAD_ERR_NO_FILE) {
  927. $file = null;
  928. } else {
  929. $file = new UploadedFile($file['tmp_name'], $file['name'],
  930. $file['type'], $file['size'], $file['error']);
  931. }
  932. }
  933. return $file;
  934. }
  935. protected function fixPhpFilesArray($data)
  936. {
  937. if (! is_array($data)) {
  938. return $data;
  939. }
  940. $keys = array_keys($data);
  941. sort($keys);
  942. if ($this->fileKeys != $keys || ! isset($data['name']) ||
  943. ! is_array($data['name'])) {
  944. return $data;
  945. }
  946. $files = $data;
  947. foreach ($this->fileKeys as $k) {
  948. unset($files[$k]);
  949. }
  950. foreach (array_keys($data['name']) as $key) {
  951. $files[$key] = $this->fixPhpFilesArray(array(
  952. 'error' => $data['error'][$key],
  953. 'name' => $data['name'][$key], 'type' => $data['type'][$key],
  954. 'tmp_name' => $data['tmp_name'][$key],
  955. 'size' => $data['size'][$key]
  956. ));
  957. }
  958. return $files;
  959. }
  960. }
  961. }
  962. namespace Symfony\Component\HttpFoundation
  963. {
  964. class ServerBag extends ParameterBag
  965. {
  966. public function getHeaders()
  967. {
  968. $headers = array();
  969. foreach ($this->parameters as $key => $value) {
  970. if ('HTTP_' === substr($key, 0, 5)) {
  971. $headers[substr($key, 5)] = $value;
  972. }
  973. }
  974. return $headers;
  975. }
  976. }
  977. }
  978. namespace Symfony\Component\HttpFoundation
  979. {
  980. class HeaderBag
  981. {
  982. protected $headers;
  983. protected $cookies;
  984. protected $cacheControl;
  985. public function __construct(array $headers = array())
  986. {
  987. $this->cacheControl = array();
  988. $this->cookies = array();
  989. $this->headers = array();
  990. foreach ($headers as $key => $values) {
  991. $this->set($key, $values);
  992. }
  993. }
  994. public function all()
  995. {
  996. return $this->headers;
  997. }
  998. public function keys()
  999. {
  1000. return array_keys($this->headers);
  1001. }
  1002. public function replace(array $headers = array())
  1003. {
  1004. $this->headers = array();
  1005. $this->add($headers);
  1006. }
  1007. public function add(array $headers)
  1008. {
  1009. foreach ($headers as $key => $values) {
  1010. $this->set($key, $values);
  1011. }
  1012. }
  1013. public function get($key, $default = null, $first = true)
  1014. {
  1015. $key = strtr(strtolower($key), '_', '-');
  1016. if (!array_key_exists($key, $this->headers)) {
  1017. if (null === $default) {
  1018. return $first ? null : array();
  1019. } else {
  1020. return $first ? $default : array($default);
  1021. }
  1022. }
  1023. if ($first) {
  1024. return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
  1025. } else {
  1026. return $this->headers[$key];
  1027. }
  1028. }
  1029. public function set($key, $values, $replace = true)
  1030. {
  1031. $key = strtr(strtolower($key), '_', '-');
  1032. if (!is_array($values)) {
  1033. $values = array($values);
  1034. }
  1035. if (true === $replace || !isset($this->headers[$key])) {
  1036. $this->headers[$key] = $values;
  1037. } else {
  1038. $this->headers[$key] = array_merge($this->headers[$key], $values);
  1039. }
  1040. if ('cache-control' === $key) {
  1041. $this->cacheControl = $this->parseCacheControl($values[0]);
  1042. }
  1043. }
  1044. public function has($key)
  1045. {
  1046. return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
  1047. }
  1048. public function contains($key, $value)
  1049. {
  1050. return in_array($value, $this->get($key, null, false));
  1051. }
  1052. public function remove($key)
  1053. {
  1054. $key = strtr(strtolower($key), '_', '-');
  1055. unset($this->headers[$key]);
  1056. if ('cache-control' === $key) {
  1057. $this->cacheControl = array();
  1058. }
  1059. }
  1060. public function setCookie(Cookie $cookie)
  1061. {
  1062. $this->cookies[$cookie->getName()] = $cookie;
  1063. }
  1064. public function removeCookie($name)
  1065. {
  1066. unset($this->cookies[$name]);
  1067. }
  1068. public function hasCookie($name)
  1069. {
  1070. return isset($this->cookies[$name]);
  1071. }
  1072. public function getCookie($name)
  1073. {
  1074. if (!$this->hasCookie($name)) {
  1075. throw new \InvalidArgumentException(sprintf('There is no cookie with name "%s".', $name));
  1076. }
  1077. return $this->cookies[$name];
  1078. }
  1079. public function getCookies()
  1080. {
  1081. return $this->cookies;
  1082. }
  1083. public function getDate($key, \DateTime $default = null)
  1084. {
  1085. if (null === $value = $this->get($key)) {
  1086. return $default;
  1087. }
  1088. if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
  1089. throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
  1090. }
  1091. return $date;
  1092. }
  1093. public function addCacheControlDirective($key, $value = true)
  1094. {
  1095. $this->cacheControl[$key] = $value;
  1096. $this->set('Cache-Control', $this->getCacheControlHeader());
  1097. }
  1098. public function hasCacheControlDirective($key)
  1099. {
  1100. return array_key_exists($key, $this->cacheControl);
  1101. }
  1102. public function getCacheControlDirective($key)
  1103. {
  1104. return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
  1105. }
  1106. public function removeCacheControlDirective($key)
  1107. {
  1108. unset($this->cacheControl[$key]);
  1109. $this->set('Cache-Control', $this->getCacheControlHeader());
  1110. }
  1111. protected function getCacheControlHeader()
  1112. {
  1113. $parts = array();
  1114. ksort($this->cacheControl);
  1115. foreach ($this->cacheControl as $key => $value) {
  1116. if (true === $value) {
  1117. $parts[] = $key;
  1118. } else {
  1119. if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
  1120. $value = '"'.$value.'"';
  1121. }
  1122. $parts[] = "$key=$value";
  1123. }
  1124. }
  1125. return implode(', ', $parts);
  1126. }
  1127. protected function parseCacheControl($header)
  1128. {
  1129. $cacheControl = array();
  1130. preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
  1131. foreach ($matches as $match) {
  1132. $cacheControl[strtolower($match[1])] = isset($match[2]) && $match[2] ? $match[2] : (isset($match[3]) ? $match[3] : true);
  1133. }
  1134. return $cacheControl;
  1135. }
  1136. }
  1137. }
  1138. namespace Symfony\Component\HttpFoundation
  1139. {
  1140. use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
  1141. use Symfony\Component\HttpFoundation\File\UploadedFile;
  1142. class Request
  1143. {
  1144. public $attributes;
  1145. public $request;
  1146. public $query;
  1147. public $server;
  1148. public $files;
  1149. public $cookies;
  1150. public $headers;
  1151. protected $content;
  1152. protected $languages;
  1153. protected $charsets;
  1154. protected $acceptableContentTypes;
  1155. protected $pathInfo;
  1156. protected $requestUri;
  1157. protected $baseUrl;
  1158. protected $basePath;
  1159. protected $method;
  1160. protected $format;
  1161. protected $session;
  1162. static protected $formats;
  1163. public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  1164. {
  1165. $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
  1166. }
  1167. public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  1168. {
  1169. $this->request = new ParameterBag($request);
  1170. $this->query = new ParameterBag($query);
  1171. $this->attributes = new ParameterBag($attributes);
  1172. $this->cookies = new ParameterBag($cookies);
  1173. $this->files = new FileBag($files);
  1174. $this->server = new ServerBag($server);
  1175. $this->headers = new HeaderBag($this->server->getHeaders());
  1176. $this->content = $content;
  1177. $this->languages = null;
  1178. $this->charsets = null;
  1179. $this->acceptableContentTypes = null;
  1180. $this->pathInfo = null;
  1181. $this->requestUri = null;
  1182. $this->baseUrl = null;
  1183. $this->basePath = null;
  1184. $this->method = null;
  1185. $this->format = null;
  1186. }
  1187. static public function createfromGlobals()
  1188. {
  1189. return new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
  1190. }
  1191. static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
  1192. {
  1193. $defaults = array(
  1194. 'SERVER_NAME' => 'localhost',
  1195. 'SERVER_PORT' => 80,
  1196. 'HTTP_HOST' => 'localhost',
  1197. 'HTTP_USER_AGENT' => 'Symfony/2.X',
  1198. 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  1199. 'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
  1200. 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  1201. 'REMOTE_ADDR' => '127.0.0.1',
  1202. 'SCRIPT_NAME' => '',
  1203. 'SCRIPT_FILENAME' => '',
  1204. );
  1205. $components = parse_url($uri);
  1206. if (isset($components['host'])) {
  1207. $defaults['SERVER_NAME'] = $components['host'];
  1208. $defaults['HTTP_HOST'] = $components['host'];
  1209. }
  1210. if (isset($components['scheme'])) {
  1211. if ('https' === $components['scheme']) {
  1212. $defaults['HTTPS'] = 'on';
  1213. $defaults['SERVER_PORT'] = 443;
  1214. }
  1215. }
  1216. if (isset($components['port'])) {
  1217. $defaults['SERVER_PORT'] = $components['port'];
  1218. $defaults['HTTP_HOST'] = $defaults['HTTP_HOST'].':'.$components['port'];
  1219. }
  1220. if (in_array(strtoupper($method), array('POST', 'PUT', 'DELETE'))) {
  1221. $request = $parameters;
  1222. $query = array();
  1223. $defaults['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  1224. } else {
  1225. $request = array();
  1226. $query = $parameters;
  1227. if (false !== $pos = strpos($uri, '?')) {
  1228. $qs = substr($uri, $pos + 1);
  1229. parse_str($qs, $params);
  1230. $query = array_merge($params, $query);
  1231. }
  1232. }
  1233. $queryString = isset($components['query']) ? html_entity_decode($components['query']) : '';
  1234. parse_str($queryString, $qs);
  1235. if (is_array($qs)) {
  1236. $query = array_replace($qs, $query);
  1237. }
  1238. $uri = $components['path'] . ($queryString ? '?'.$queryString : '');
  1239. $server = array_replace($defaults, $server, array(
  1240. 'REQUEST_METHOD' => strtoupper($method),
  1241. 'PATH_INFO' => '',
  1242. 'REQUEST_URI' => $uri,
  1243. 'QUERY_STRING' => $queryString,
  1244. ));
  1245. return new static($query, $request, array(), $cookies, $files, $server, $content);
  1246. }
  1247. public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
  1248. {
  1249. $dup = clone $this;
  1250. if ($query !== null) {
  1251. $dup->query = new ParameterBag($query);
  1252. }
  1253. if ($request !== null) {
  1254. $dup->request = new ParameterBag($request);
  1255. }
  1256. if ($attributes !== null) {
  1257. $dup->attributes = new ParameterBag($attributes);
  1258. }
  1259. if ($cookies !== null) {
  1260. $dup->cookies = new ParameterBag($cookies);
  1261. }
  1262. if ($files !== null) {
  1263. $dup->files = new FileBag($files);
  1264. }
  1265. if ($server !== null) {
  1266. $dup->server = new ServerBag($server);
  1267. $dup->headers = new HeaderBag($dup->server->getHeaders());
  1268. }
  1269. $this->languages = null;
  1270. $this->charsets = null;
  1271. $this->acceptableContentTypes = null;
  1272. $this->pathInfo = null;
  1273. $this->requestUri = null;
  1274. $this->baseUrl = null;
  1275. $this->basePath = null;
  1276. $this->method = null;
  1277. $this->format = null;
  1278. return $dup;
  1279. }
  1280. public function __clone()
  1281. {
  1282. $this->query = clone $this->query;
  1283. $this->request = clone $this->request;
  1284. $this->attributes = clone $this->attributes;
  1285. $this->cookies = clone $this->cookies;
  1286. $this->files = clone $this->files;
  1287. $this->server = clone $this->server;
  1288. $this->headers = clone $this->headers;
  1289. }
  1290. public function overrideGlobals()
  1291. {
  1292. $_GET = $this->query->all();
  1293. $_POST = $this->request->all();
  1294. $_SERVER = $this->server->all();
  1295. $_COOKIE = $this->cookies->all();
  1296. foreach ($this->headers->all() as $key => $value) {
  1297. $_SERVER['HTTP_'.strtoupper(str_replace('-', '_', $key))] = implode(', ', $value);
  1298. }
  1299. $_REQUEST = array_merge($_GET, $_POST);
  1300. }
  1301. public function get($key, $default = null)
  1302. {
  1303. return $this->query->get($key, $this->attributes->get($key, $this->request->get($key, $default)));
  1304. }
  1305. public function getSession()
  1306. {
  1307. return $this->session;
  1308. }
  1309. public function hasSession()
  1310. {
  1311. return $this->cookies->has(session_name());
  1312. }
  1313. public function setSession(Session $session)
  1314. {
  1315. $this->session = $session;
  1316. }
  1317. public function getClientIp($proxy = false)
  1318. {
  1319. if ($proxy) {
  1320. if ($this->server->has('HTTP_CLIENT_IP')) {
  1321. return $this->server->get('HTTP_CLIENT_IP');
  1322. } elseif ($this->server->has('HTTP_X_FORWARDED_FOR')) {
  1323. return $this->server->get('HTTP_X_FORWARDED_FOR');
  1324. }
  1325. }
  1326. return $this->server->get('REMOTE_ADDR');
  1327. }
  1328. public function getScriptName()
  1329. {
  1330. return $this->server->get('SCRIPT_NAME', $this->server->get('ORIG_SCRIPT_NAME', ''));
  1331. }
  1332. public function getPathInfo()
  1333. {
  1334. if (null === $this->pathInfo) {
  1335. $this->pathInfo = $this->preparePathInfo();
  1336. }
  1337. return $this->pathInfo;
  1338. }
  1339. public function getBasePath()
  1340. {
  1341. if (null === $this->basePath) {
  1342. $this->basePath = $this->prepareBasePath();
  1343. }
  1344. return $this->basePath;
  1345. }
  1346. public function getBaseUrl()
  1347. {
  1348. if (null === $this->baseUrl) {
  1349. $this->baseUrl = $this->prepareBaseUrl();
  1350. }
  1351. return $this->baseUrl;
  1352. }
  1353. public function getScheme()
  1354. {
  1355. return ($this->server->get('HTTPS') == 'on') ? 'https' : 'http';
  1356. }
  1357. public function getPort()
  1358. {
  1359. return $this->server->get('SERVER_PORT');
  1360. }
  1361. public function getHttpHost()
  1362. {
  1363. $host = $this->headers->get('HOST');
  1364. if (!empty($host)) {
  1365. return $host;
  1366. }
  1367. $scheme = $this->getScheme();
  1368. $name = $this->server->get('SERVER_NAME');
  1369. $port = $this->getPort();
  1370. if (('http' == $scheme && $port == 80) || ('https' == $scheme && $port == 443)) {
  1371. return $name;
  1372. } else {
  1373. return $name.':'.$port;
  1374. }
  1375. }
  1376. public function getRequestUri()
  1377. {
  1378. if (null === $this->requestUri) {
  1379. $this->requestUri = $this->prepareRequestUri();
  1380. }
  1381. return $this->requestUri;
  1382. }
  1383. public function getUri()
  1384. {
  1385. $qs = $this->getQueryString();
  1386. if (null !== $qs) {
  1387. $qs = '?'.$qs;
  1388. }
  1389. return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
  1390. }
  1391. public function getUriForPath($path)
  1392. {
  1393. return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$path;
  1394. }
  1395. public function getQueryString()
  1396. {
  1397. if (!$qs = $this->server->get('QUERY_STRING')) {
  1398. return null;
  1399. }
  1400. $parts = array();
  1401. $order = array();
  1402. foreach (explode('&', $qs) as $segment) {
  1403. if (false === strpos($segment, '=')) {
  1404. $parts[] = $segment;
  1405. $order[] = $segment;
  1406. } else {
  1407. $tmp = explode('=', urldecode($segment), 2);
  1408. $parts[] = urlencode($tmp[0]).'='.urlencode($tmp[1]);
  1409. $order[] = $tmp[0];
  1410. }
  1411. }
  1412. array_multisort($order, SORT_ASC, $parts);
  1413. return implode('&', $parts);
  1414. }
  1415. public function isSecure()
  1416. {
  1417. return (
  1418. (strtolower($this->server->get('HTTPS')) == 'on' || $this->server->get('HTTPS') == 1)
  1419. ||
  1420. (strtolower($this->headers->get('SSL_HTTPS')) == 'on' || $this->headers->get('SSL_HTTPS') == 1)
  1421. ||
  1422. (strtolower($this->headers->get('X_FORWARDED_PROTO')) == 'https')
  1423. );
  1424. }
  1425. public function getHost()
  1426. {
  1427. if ($host = $this->headers->get('X_FORWARDED_HOST')) {
  1428. $elements = explode(',', $host);
  1429. $host = trim($elements[count($elements) - 1]);
  1430. } else {
  1431. if (!$host = $this->headers->get('HOST')) {
  1432. if (!$host = $this->server->get('SERVER_NAME')) {
  1433. $host = $this->server->get('SERVER_ADDR', '');
  1434. }
  1435. }
  1436. }
  1437. $elements = explode(':', $host);
  1438. return trim($elements[0]);
  1439. }
  1440. public function setMethod($method)
  1441. {
  1442. $this->method = null;
  1443. $this->server->set('REQUEST_METHOD', $method);
  1444. }
  1445. public function getMethod()
  1446. {
  1447. if (null === $this->method) {
  1448. $this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
  1449. if ('POST' === $this->method) {
  1450. $this->method = strtoupper($this->request->get('_method', 'POST'));
  1451. }
  1452. }
  1453. return $this->method;
  1454. }
  1455. public function getMimeType($format)
  1456. {
  1457. if (null === static::$formats) {
  1458. static::initializeFormats();
  1459. }
  1460. return isset(static::$formats[$format]) ? static::$formats[$format][0] : null;
  1461. }
  1462. public function getFormat($mimeType)
  1463. {
  1464. if (null === static::$formats) {
  1465. static::initializeFormats();
  1466. }
  1467. foreach (static::$formats as $format => $mimeTypes) {
  1468. if (in_array($mimeType, (array) $mimeTypes)) {
  1469. return $format;
  1470. }
  1471. }
  1472. return null;
  1473. }
  1474. public function setFormat($format, $mimeTypes)
  1475. {
  1476. if (null === static::$formats) {
  1477. static::initializeFormats();
  1478. }
  1479. static::$formats[$format] = is_array($mimeTypes) ? $mimeTypes : array($mimeTypes);
  1480. }
  1481. public function getRequestFormat()
  1482. {
  1483. if (null === $this->format) {
  1484. $this->format = $this->get('_format', 'html');
  1485. }
  1486. return $this->format;
  1487. }
  1488. public function setRequestFormat($format)
  1489. {
  1490. $this->format = $format;
  1491. }
  1492. public function isMethodSafe()
  1493. {
  1494. return in_array($this->getMethod(), array('GET', 'HEAD'));
  1495. }
  1496. public function getContent($asResource = false)
  1497. {
  1498. if (false === $this->content || (true === $asResource && null !== $this->content)) {
  1499. throw new \LogicException('getContent() can only be called once when using the resource return type.');
  1500. }
  1501. if (true === $asResource) {
  1502. $this->content = false;
  1503. return fopen('php://input', 'rb');
  1504. }
  1505. if (null === $this->content) {
  1506. $this->content = file_get_contents('php://input');
  1507. }
  1508. return $this->content;
  1509. }
  1510. public function getETags()
  1511. {
  1512. return preg_split('/\s*,\s*/', $this->headers->get('if_none_match'), null, PREG_SPLIT_NO_EMPTY);
  1513. }
  1514. public function isNoCache()
  1515. {
  1516. return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
  1517. }
  1518. public function getPreferredLanguage(array $locales = null)
  1519. {
  1520. $preferredLanguages = $this->getLanguages();
  1521. if (null === $locales) {
  1522. return isset($preferredLanguages[0]) ? $preferredLanguages[0] : null;
  1523. }
  1524. if (!$preferredLanguages) {
  1525. return $locales[0];
  1526. }
  1527. $preferredLanguages = array_values(array_intersect($preferredLanguages, $locales));
  1528. return isset($preferredLanguages[0]) ? $preferredLanguages[0] : $locales[0];
  1529. }
  1530. public function getLanguages()
  1531. {
  1532. if (null !== $this->languages) {
  1533. return $this->languages;
  1534. }
  1535. $languages = $this->splitHttpAcceptHeader($this->headers->get('Accept-Language'));
  1536. foreach ($languages as $lang) {
  1537. if (strstr($lang, '-')) {
  1538. $codes = explode('-', $lang);
  1539. if ($codes[0] == 'i') {
  1540. if (count($codes) > 1) {
  1541. $lang = $codes[1];
  1542. }
  1543. } else {
  1544. for ($i = 0, $max = count($codes); $i < $max; $i++) {
  1545. if ($i == 0) {
  1546. $lang = strtolower($codes[0]);
  1547. } else {
  1548. $lang .= '_'.strtoupper($codes[$i]);
  1549. }
  1550. }
  1551. }
  1552. }
  1553. $this->languages[] = $lang;
  1554. }
  1555. return $this->languages;
  1556. }
  1557. public function getCharsets()
  1558. {
  1559. if (null !== $this->charsets) {
  1560. return $this->charsets;
  1561. }
  1562. return $this->charsets = $this->splitHttpAcceptHeader($this->headers->get('Accept-Charset'));
  1563. }
  1564. public function getAcceptableContentTypes()
  1565. {
  1566. if (null !== $this->acceptableContentTypes) {
  1567. return $this->acceptableContentTypes;
  1568. }
  1569. return $this->acceptableContentTypes = $this->splitHttpAcceptHeader($this->headers->get('Accept'));
  1570. }
  1571. public function isXmlHttpRequest()
  1572. {
  1573. return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
  1574. }
  1575. public function splitHttpAcceptHeader($header)
  1576. {
  1577. if (!$header) {
  1578. return array();
  1579. }
  1580. $values = array();
  1581. foreach (array_filter(explode(',', $header)) as $value) {
  1582. if ($pos = strpos($value, ';')) {
  1583. $q = (float) trim(substr($value, strpos($value, '=') + 1));
  1584. $value = trim(substr($value, 0, $pos));
  1585. } else {
  1586. $q = 1;
  1587. }
  1588. if (0 < $q) {
  1589. $values[trim($value)] = $q;
  1590. }
  1591. }
  1592. arsort($values);
  1593. return array_keys($values);
  1594. }
  1595. protected function prepareRequestUri()
  1596. {
  1597. $requestUri = '';
  1598. if ($this->headers->has('X_REWRITE_URL')) {
  1599. $requestUri = $this->headers->get('X_REWRITE_URL');
  1600. } elseif ($this->server->get('IIS_WasUrlRewritten') == '1' && $this->server->get('UNENCODED_URL') != '') {
  1601. $requestUri = $this->server->get('UNENCODED_URL');
  1602. } elseif ($this->server->has('REQUEST_URI')) {
  1603. $requestUri = $this->server->get('REQUEST_URI');
  1604. $schemeAndHttpHost = $this->getScheme().'://'.$this->getHttpHost();
  1605. if (strpos($requestUri, $schemeAndHttpHost) === 0) {
  1606. $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
  1607. }
  1608. } elseif ($this->server->has('ORIG_PATH_INFO')) {
  1609. $requestUri = $this->server->get('ORIG_PATH_INFO');
  1610. if ($this->server->get('QUERY_STRING')) {
  1611. $requestUri .= '?'.$this->server->get('QUERY_STRING');
  1612. }
  1613. }
  1614. return $requestUri;
  1615. }
  1616. protected function prepareBaseUrl()
  1617. {
  1618. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  1619. if (basename($this->server->get('SCRIPT_NAME')) === $filename) {
  1620. $baseUrl = $this->server->get('SCRIPT_NAME');
  1621. } elseif (basename($this->server->get('PHP_SELF')) === $filename) {
  1622. $baseUrl = $this->server->get('PHP_SELF');
  1623. } elseif (basename($this->server->get('ORIG_SCRIPT_NAME')) === $filename) {
  1624. $baseUrl = $this->server->get('ORIG_SCRIPT_NAME'); } else {
  1625. $path = $this->server->get('PHP_SELF', '');
  1626. $file = $this->server->get('SCRIPT_FILENAME', '');
  1627. $segs = explode('/', trim($file, '/'));
  1628. $segs = array_reverse($segs);
  1629. $index = 0;
  1630. $last = count($segs);
  1631. $baseUrl = '';
  1632. do {
  1633. $seg = $segs[$index];
  1634. $baseUrl = '/'.$seg.$baseUrl;
  1635. ++$index;
  1636. } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
  1637. }
  1638. $requestUri = $this->getRequestUri();
  1639. if ($baseUrl && 0 === strpos($requestUri, $baseUrl)) {
  1640. return $baseUrl;
  1641. }
  1642. if ($baseUrl && 0 === strpos($requestUri, dirname($baseUrl))) {
  1643. return rtrim(dirname($baseUrl), '/');
  1644. }
  1645. $truncatedRequestUri = $requestUri;
  1646. if (($pos = strpos($requestUri, '?')) !== false) {
  1647. $truncatedRequestUri = substr($requestUri, 0, $pos);
  1648. }
  1649. $basename = basename($baseUrl);
  1650. if (empty($basename) || !strpos($truncatedRequestUri, $basename)) {
  1651. return '';
  1652. }
  1653. if ((strlen($requestUri) >= strlen($baseUrl)) && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0))) {
  1654. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  1655. }
  1656. return rtrim($baseUrl, '/');
  1657. }
  1658. protected function prepareBasePath()
  1659. {
  1660. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  1661. $baseUrl = $this->getBaseUrl();
  1662. if (empty($baseUrl)) {
  1663. return '';
  1664. }
  1665. if (basename($baseUrl) === $filename) {
  1666. $basePath = dirname($baseUrl);
  1667. } else {
  1668. $basePath = $baseUrl;
  1669. }
  1670. if ('\\' === DIRECTORY_SEPARATOR) {
  1671. $basePath = str_replace('\\', '/', $basePath);
  1672. }
  1673. return rtrim($basePath, '/');
  1674. }
  1675. protected function preparePathInfo()
  1676. {
  1677. $baseUrl = $this->getBaseUrl();
  1678. if (null === ($requestUri = $this->getRequestUri())) {
  1679. return '';
  1680. }
  1681. $pathInfo = '';
  1682. if ($pos = strpos($requestUri, '?')) {
  1683. $requestUri = substr($requestUri, 0, $pos);
  1684. }
  1685. if ((null !== $baseUrl) && (false === ($pathInfo = substr($requestUri, strlen($baseUrl))))) {
  1686. return '';
  1687. } elseif (null === $baseUrl) {
  1688. return $requestUri;
  1689. }
  1690. return (string) $pathInfo;
  1691. }
  1692. static protected function initializeFormats()
  1693. {
  1694. static::$formats = array(
  1695. 'txt' => array('text/plain'),
  1696. 'js' => array('application/javascript', 'application/x-javascript', 'text/javascript'),
  1697. 'css' => array('text/css'),
  1698. 'json' => array('application/json', 'application/x-json'),
  1699. 'xml' => array('text/xml', 'application/xml', 'application/x-xml'),
  1700. 'rdf' => array('application/rdf+xml'),
  1701. 'atom' => array('application/atom+xml'),
  1702. );
  1703. }
  1704. }
  1705. }
  1706. namespace Symfony\Component\HttpFoundation
  1707. {
  1708. class ApacheRequest extends Request
  1709. {
  1710. protected function prepareRequestUri()
  1711. {
  1712. return $this->server->get('REQUEST_URI');
  1713. }
  1714. protected function prepareBaseUrl()
  1715. {
  1716. return $this->server->get('SCRIPT_NAME');
  1717. }
  1718. protected function preparePathInfo()
  1719. {
  1720. return $this->server->get('PATH_INFO');
  1721. }
  1722. }
  1723. }
  1724. namespace Symfony\Component\ClassLoader
  1725. {
  1726. class ClassCollectionLoader
  1727. {
  1728. static protected $loaded;
  1729. static public function load($classes, $cacheDir, $name, $autoReload, $adaptive = false)
  1730. {
  1731. if (isset(self::$loaded[$name])) {
  1732. return;
  1733. }
  1734. self::$loaded[$name] = true;
  1735. $classes = array_unique($classes);
  1736. if ($adaptive) {
  1737. $classes = array_diff($classes, get_declared_classes(), get_declared_interfaces());
  1738. $name = $name.'-'.substr(md5(implode('|', $classes)), 0, 5);
  1739. }
  1740. $cache = $cacheDir.'/'.$name.'.php';
  1741. $reload = false;
  1742. if ($autoReload) {
  1743. $metadata = $cacheDir.'/'.$name.'.meta';
  1744. if (!file_exists($metadata) || !file_exists($cache)) {
  1745. $reload = true;
  1746. } else {
  1747. $time = filemtime($cache);
  1748. $meta = unserialize(file_get_contents($metadata));
  1749. if ($meta[1] != $classes) {
  1750. $reload = true;
  1751. } else {
  1752. foreach ($meta[0] as $resource) {
  1753. if (!file_exists($resource) || filemtime($resource) > $time) {
  1754. $reload = true;
  1755. break;
  1756. }
  1757. }
  1758. }
  1759. }
  1760. }
  1761. if (!$reload && file_exists($cache)) {
  1762. require_once $cache;
  1763. return;
  1764. }
  1765. $files = array();
  1766. $content = '';
  1767. foreach ($classes as $class) {
  1768. if (!class_exists($class) && !interface_exists($class)) {
  1769. throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
  1770. }
  1771. $r = new \ReflectionClass($class);
  1772. $files[] = $r->getFileName();
  1773. $c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
  1774. if (!$r->inNamespace()) {
  1775. $c = "\nnamespace\n{\n$c\n}\n";
  1776. } else {
  1777. $c = self::fixNamespaceDeclarations('<?php '.$c);
  1778. $c = preg_replace('/^\s*<\?php/', '', $c);
  1779. }
  1780. $content .= $c;
  1781. }
  1782. if (!is_dir(dirname($cache))) {
  1783. mkdir(dirname($cache), 0777, true);
  1784. }
  1785. self::writeCacheFile($cache, self::stripComments('<?php '.$content));
  1786. if ($autoReload) {
  1787. self::writeCacheFile($metadata, serialize(array($files, $classes)));
  1788. }
  1789. }
  1790. static public function fixNamespaceDeclarations($source)
  1791. {
  1792. if (!function_exists('token_get_all')) {
  1793. return $source;
  1794. }
  1795. $output = '';
  1796. $inNamespace = false;
  1797. $tokens = token_get_all($source);
  1798. while ($token = array_shift($tokens)) {
  1799. if (is_string($token)) {
  1800. $output .= $token;
  1801. } elseif (T_NAMESPACE === $token[0]) {
  1802. if ($inNamespace) {
  1803. $output .= "}\n";
  1804. }
  1805. $output .= $token[1];
  1806. while (($t = array_shift($tokens)) && is_array($t) && in_array($t[0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
  1807. $output .= $t[1];
  1808. }
  1809. if (is_string($t) && '{' === $t) {
  1810. $inNamespace = false;
  1811. array_unshift($tokens, $t);
  1812. } else {
  1813. $output .= "\n{";
  1814. $inNamespace = true;
  1815. }
  1816. } else {
  1817. $output .= $token[1];
  1818. }
  1819. }
  1820. if ($inNamespace) {
  1821. $output .= "}\n";
  1822. }
  1823. return $output;
  1824. }
  1825. static protected function writeCacheFile($file, $content)
  1826. {
  1827. $tmpFile = tempnam(dirname($file), basename($file));
  1828. if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
  1829. chmod($file, 0644);
  1830. return;
  1831. }
  1832. throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
  1833. }
  1834. static protected function stripComments($source)
  1835. {
  1836. if (!function_exists('token_get_all')) {
  1837. return $source;
  1838. }
  1839. $output = '';
  1840. foreach (token_get_all($source) as $token) {
  1841. if (is_string($token)) {
  1842. $output .= $token;
  1843. } elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
  1844. $output .= $token[1];
  1845. }
  1846. }
  1847. $output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
  1848. return $output;
  1849. }
  1850. }
  1851. }
  1852. namespace Symfony\Component\ClassLoader
  1853. {
  1854. class UniversalClassLoader
  1855. {
  1856. protected $namespaces = array();
  1857. protected $prefixes = array();
  1858. protected $namespaceFallback = array();
  1859. protected $prefixFallback = array();
  1860. public function getNamespaces()
  1861. {
  1862. return $this->namespaces;
  1863. }
  1864. public function getPrefixes()
  1865. {
  1866. return $this->prefixes;
  1867. }
  1868. public function getNamespaceFallback()
  1869. {
  1870. return $this->namespaceFallback;
  1871. }
  1872. public function getPrefixFallback()
  1873. {
  1874. return $this->prefixFallback;
  1875. }
  1876. public function registerNamespaceFallback($dirs)
  1877. {
  1878. $this->namespaceFallback = (array) $dirs;
  1879. }
  1880. public function registerPrefixFallback($dirs)
  1881. {
  1882. $this->prefixFallback = (array) $dirs;
  1883. }
  1884. public function registerNamespaces(array $namespaces)
  1885. {
  1886. foreach ($namespaces as $namespace => $locations) {
  1887. $this->namespaces[$namespace] = (array) $locations;
  1888. }
  1889. }
  1890. public function registerNamespace($namespace, $paths)
  1891. {
  1892. $this->namespaces[$namespace] = (array) $paths;
  1893. }
  1894. public function registerPrefixes(array $classes)
  1895. {
  1896. foreach ($classes as $prefix => $locations) {
  1897. $this->prefixes[$prefix] = (array) $locations;
  1898. }
  1899. }
  1900. public function registerPrefix($prefix, $paths)
  1901. {
  1902. $this->prefixes[$prefix] = (array) $paths;
  1903. }
  1904. public function register($prepend = false)
  1905. {
  1906. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  1907. }
  1908. public function loadClass($class)
  1909. {
  1910. $class = ltrim($class, '\\');
  1911. if (false !== ($pos = strrpos($class, '\\'))) {
  1912. $namespace = substr($class, 0, $pos);
  1913. foreach ($this->namespaces as $ns => $dirs) {
  1914. foreach ($dirs as $dir) {
  1915. if (0 === strpos($namespace, $ns)) {
  1916. $className = substr($class, $pos + 1);
  1917. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
  1918. if (file_exists($file)) {
  1919. require $file;
  1920. return;
  1921. }
  1922. }
  1923. }
  1924. }
  1925. foreach ($this->namespaceFallback as $dir) {
  1926. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
  1927. if (file_exists($file)) {
  1928. require $file;
  1929. return;
  1930. }
  1931. }
  1932. } else {
  1933. foreach ($this->prefixes as $prefix => $dirs) {
  1934. foreach ($dirs as $dir) {
  1935. if (0 === strpos($class, $prefix)) {
  1936. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  1937. if (file_exists($file)) {
  1938. require $file;
  1939. return;
  1940. }
  1941. }
  1942. }
  1943. }
  1944. foreach ($this->prefixFallback as $dir) {
  1945. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  1946. if (file_exists($file)) {
  1947. require $file;
  1948. return;
  1949. }
  1950. }
  1951. }
  1952. }
  1953. }
  1954. }
  1955. namespace Symfony\Component\ClassLoader
  1956. {
  1957. class MapFileClassLoader
  1958. {
  1959. protected $map = array();
  1960. public function __construct($file)
  1961. {
  1962. $this->map = require $file;
  1963. }
  1964. public function register($prepend = false)
  1965. {
  1966. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  1967. }
  1968. public function loadClass($class)
  1969. {
  1970. if ('\\' === $class[0]) {
  1971. $class = substr($class, 1);
  1972. }
  1973. if (isset($this->map[$class])) {
  1974. require $this->map[$class];
  1975. }
  1976. }
  1977. }
  1978. }