From 3734c0e01e9e454e3dacbc43ec85f3796dbaea94 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 10 Jan 2011 08:01:04 +0100 Subject: [PATCH] updated bootstrap file --- .../Component/HttpKernel/bootstrap.php | 321 ++++++++++++------ 1 file changed, 221 insertions(+), 100 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/bootstrap.php b/src/Symfony/Component/HttpKernel/bootstrap.php index e430c673b26e..4e38d94f097d 100644 --- a/src/Symfony/Component/HttpKernel/bootstrap.php +++ b/src/Symfony/Component/HttpKernel/bootstrap.php @@ -5,61 +5,92 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Console\Application; use Symfony\Component\Finder\Finder; -abstract class Bundle extends ContainerAware implements BundleInterface { +abstract class Bundle extends ContainerAware implements BundleInterface +{ protected $name; protected $namespacePrefix; protected $path; protected $reflection; - public function boot() { } - public function shutdown() { } - public function getName() { + public function boot() + { + } + public function shutdown() + { + } + public function getName() + { if (null === $this->name) { - $this->initReflection(); } - return $this->name; } - public function getNamespacePrefix() { + $this->initReflection(); + } + return $this->name; + } + public function getNamespacePrefix() + { if (null === $this->name) { - $this->initReflection(); } - return $this->namespacePrefix; } - public function getPath() { + $this->initReflection(); + } + return $this->namespacePrefix; + } + public function getPath() + { if (null === $this->name) { - $this->initReflection(); } - return $this->path; } - public function getReflection() { + $this->initReflection(); + } + return $this->path; + } + public function getReflection() + { if (null === $this->name) { - $this->initReflection(); } - return $this->reflection; } - public function registerExtensions(ContainerBuilder $container) { + $this->initReflection(); + } + return $this->reflection; + } + public function registerExtensions(ContainerBuilder $container) + { if (!$dir = realpath($this->getPath().'/DependencyInjection')) { - return array(); } + return; + } $finder = new Finder(); $finder->files()->name('*Extension.php')->in($dir); $prefix = $this->namespacePrefix.'\\'.$this->name.'\\DependencyInjection'; foreach ($finder as $file) { - $class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.basename($file, '.php'); - if ('Extension' === substr($class, -9)) { - $container->registerExtension(new $class()); } } } - public function registerCommands(Application $application) { + $class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php'); + $container->registerExtension(new $class()); + } + } + public function registerCommands(Application $application) + { if (!$dir = realpath($this->getPath().'/Command')) { - return; } + return; + } $finder = new Finder(); $finder->files()->name('*Command.php')->in($dir); $prefix = $this->namespacePrefix.'\\'.$this->name.'\\Command'; foreach ($finder as $file) { - $r = new \ReflectionClass($prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.basename($file, '.php')); + $r = new \ReflectionClass($prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php')); if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) { - $application->add($r->newInstance()); } } } - protected function initReflection() { + $application->add($r->newInstance()); + } + } + } + protected function initReflection() + { $tmp = dirname(str_replace('\\', '/', get_class($this))); $this->namespacePrefix = str_replace('/', '\\', dirname($tmp)); $this->name = basename($tmp); $this->reflection = new \ReflectionObject($this); - $this->path = dirname($this->reflection->getFilename()); } } + $this->path = str_replace('\\', '/', dirname($this->reflection->getFilename())); + } +} namespace Symfony\Component\HttpKernel\Bundle; -interface BundleInterface { +interface BundleInterface +{ function boot(); - function shutdown(); } + function shutdown(); +} namespace Symfony\Component\HttpKernel\Debug; -class ErrorHandler { +class ErrorHandler +{ protected $levels = array( E_WARNING => 'Warning', E_NOTICE => 'Notice', @@ -70,140 +101,230 @@ class ErrorHandler { E_RECOVERABLE_ERROR => 'Catchable Fatal Error', ); protected $level; - public function __construct($level = null) { - $this->level = null === $level ? error_reporting() : $level; } - public function register() { - set_error_handler(array($this, 'handle')); } - public function handle($level, $message, $file, $line, $context) { + public function __construct($level = null) + { + $this->level = null === $level ? error_reporting() : $level; + } + public function register() + { + set_error_handler(array($this, 'handle')); + } + public function handle($level, $message, $file, $line, $context) + { if (0 === $this->level) { - return false; } + return false; + } if (error_reporting() & $level && $this->level & $level) { - throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line)); } - return false; } } + throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line)); + } + return false; + } +} namespace Symfony\Component\HttpKernel; -class ClassCollectionLoader { +class ClassCollectionLoader +{ static protected $loaded; - static public function load($classes, $cacheDir, $name, $autoReload, $adaptive = false) { + static public function load($classes, $cacheDir, $name, $autoReload, $adaptive = false) + { if (isset(self::$loaded[$name])) { - return; } + return; + } self::$loaded[$name] = true; $classes = array_unique($classes); if ($adaptive) { $classes = array_diff($classes, get_declared_classes(), get_declared_interfaces()); - $name = $name.'-'.substr(md5(implode('|', $classes)), 0, 5); } + $name = $name.'-'.substr(md5(implode('|', $classes)), 0, 5); + } $cache = $cacheDir.'/'.$name.'.php'; $reload = false; if ($autoReload) { $metadata = $cacheDir.'/'.$name.'.meta'; if (!file_exists($metadata) || !file_exists($cache)) { - $reload = true; } else { + $reload = true; + } else { $time = filemtime($cache); $meta = unserialize(file_get_contents($metadata)); if ($meta[1] != $classes) { - $reload = true; } else { + $reload = true; + } else { foreach ($meta[0] as $resource) { if (!file_exists($resource) || filemtime($resource) > $time) { $reload = true; - break; } } } } } + break; + } + } + } + } + } if (!$reload && file_exists($cache)) { require_once $cache; - return; } + return; + } $files = array(); $content = ''; foreach ($classes as $class) { if (!class_exists($class) && !interface_exists($class)) { - throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class)); } + throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class)); + } $r = new \ReflectionClass($class); $files[] = $r->getFileName(); - $content .= preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName())); } + $content .= preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName())); + } if (!is_dir(dirname($cache))) { - mkdir(dirname($cache), 0777, true); } + mkdir(dirname($cache), 0777, true); + } self::writeCacheFile($cache, Kernel::stripComments('parameterBag = null === $parameterBag ? new ParameterBag() : $parameterBag; $this->services = array(); - $this->set('service_container', $this); } - public function freeze() { + $this->set('service_container', $this); + } + public function freeze() + { $this->parameterBag->resolve(); - $this->parameterBag = new FrozenParameterBag($this->parameterBag->all()); } - public function isFrozen() { - return $this->parameterBag instanceof FrozenParameterBag; } - public function getParameterBag() { - return $this->parameterBag; } - public function getParameter($name) { - return $this->parameterBag->get($name); } - public function hasParameter($name) { - return $this->parameterBag->has($name); } - public function setParameter($name, $value) { - $this->parameterBag->set($name, $value); } - public function set($id, $service) { - $this->services[$id] = $service; } - public function has($id) { - return isset($this->services[$id]) || method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service'); } - public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) { - $id = (string) $id; + $this->parameterBag = new FrozenParameterBag($this->parameterBag->all()); + } + public function isFrozen() + { + return $this->parameterBag instanceof FrozenParameterBag; + } + public function getParameterBag() + { + return $this->parameterBag; + } + public function getParameter($name) + { + return $this->parameterBag->get($name); + } + public function hasParameter($name) + { + return $this->parameterBag->has($name); + } + public function setParameter($name, $value) + { + $this->parameterBag->set($name, $value); + } + public function set($id, $service) + { + $this->services[strtolower($id)] = $service; + } + public function has($id) + { + $id = strtolower($id); + return isset($this->services[$id]) || method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service'); + } + public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) + { + static $loading = array(); + $id = strtolower($id); if (isset($this->services[$id])) { - return $this->services[$id]; } + return $this->services[$id]; + } + if (isset($loading[$id])) { + throw new \LogicException(sprintf('Circular reference detected for service "%s" (services currently loading: %s).', $id, implode(', ', array_keys($loading)))); + } if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')) { - return $this->$method(); } + $loading[$id] = true; + $service = $this->$method(); + unset($loading[$id]); + return $service; + } if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) { - throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $id)); } } - public function getServiceIds() { + throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $id)); + } + } + public function getServiceIds() + { $ids = array(); $r = new \ReflectionClass($this); foreach ($r->getMethods() as $method) { if (preg_match('/^get(.+)Service$/', $name = $method->getName(), $match)) { - $ids[] = self::underscore($match[1]); } } - return array_merge($ids, array_keys($this->services)); } - static public function camelize($id) { - return preg_replace(array('/(?:^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\1')", "'_'.strtoupper('\\1')"), $id); } - static public function underscore($id) { - return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($id, '_', '.'))); } } + $ids[] = self::underscore($match[1]); + } + } + return array_merge($ids, array_keys($this->services)); + } + static public function camelize($id) + { + return preg_replace(array('/(?:^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\1')", "'_'.strtoupper('\\1')"), $id); + } + static public function underscore($id) + { + return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($id, '_', '.'))); + } +} namespace Symfony\Component\DependencyInjection; -interface ContainerAwareInterface { - function setContainer(ContainerInterface $container = null); } +interface ContainerAwareInterface +{ + function setContainer(ContainerInterface $container = null); +} namespace Symfony\Component\DependencyInjection; -interface ContainerInterface { +interface ContainerInterface +{ const EXCEPTION_ON_INVALID_REFERENCE = 1; const NULL_ON_INVALID_REFERENCE = 2; const IGNORE_ON_INVALID_REFERENCE = 3; function set($id, $service); function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE); - function has($id); } + function has($id); +} namespace Symfony\Component\DependencyInjection\ParameterBag; -class FrozenParameterBag extends ParameterBag { - public function __construct(array $parameters = array()) { +class FrozenParameterBag extends ParameterBag +{ + public function __construct(array $parameters = array()) + { foreach ($parameters as $key => $value) { - $this->parameters[strtolower($key)] = $value; } } - public function clear() { - throw new \LogicException('Impossible to call clear() on a frozen ParameterBag.'); } - public function add(array $parameters) { - throw new \LogicException('Impossible to call add() on a frozen ParameterBag.'); } - public function set($name, $value) { - throw new \LogicException('Impossible to call set() on a frozen ParameterBag.'); } } + $this->parameters[strtolower($key)] = $value; + } + } + public function clear() + { + throw new \LogicException('Impossible to call clear() on a frozen ParameterBag.'); + } + public function add(array $parameters) + { + throw new \LogicException('Impossible to call add() on a frozen ParameterBag.'); + } + public function set($name, $value) + { + throw new \LogicException('Impossible to call set() on a frozen ParameterBag.'); + } +} namespace Symfony\Component\DependencyInjection\ParameterBag; -interface ParameterBagInterface { +interface ParameterBagInterface +{ function clear(); function add(array $parameters); function all(); function get($name); function set($name, $value); - function has($name); } + function has($name); +} namespace Symfony\Component\DependencyInjection; -interface TaggedContainerInterface extends ContainerInterface { - function findTaggedServiceIds($name); } +interface TaggedContainerInterface extends ContainerInterface +{ + function findTaggedServiceIds($name); +}