PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/cart/bootstrap.php

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