bootstrap.php 70 KB

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