updated bootstrap file

This commit is contained in:
Fabien Potencier 2011-01-10 08:01:04 +01:00
parent dedf29ffda
commit 3734c0e01e

View File

@ -5,61 +5,92 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
abstract class Bundle extends ContainerAware implements BundleInterface { abstract class Bundle extends ContainerAware implements BundleInterface
{
protected $name; protected $name;
protected $namespacePrefix; protected $namespacePrefix;
protected $path; protected $path;
protected $reflection; protected $reflection;
public function boot() { } public function boot()
public function shutdown() { } {
public function getName() { }
public function shutdown()
{
}
public function getName()
{
if (null === $this->name) { if (null === $this->name) {
$this->initReflection(); } $this->initReflection();
return $this->name; } }
public function getNamespacePrefix() { return $this->name;
}
public function getNamespacePrefix()
{
if (null === $this->name) { if (null === $this->name) {
$this->initReflection(); } $this->initReflection();
return $this->namespacePrefix; } }
public function getPath() { return $this->namespacePrefix;
}
public function getPath()
{
if (null === $this->name) { if (null === $this->name) {
$this->initReflection(); } $this->initReflection();
return $this->path; } }
public function getReflection() { return $this->path;
}
public function getReflection()
{
if (null === $this->name) { if (null === $this->name) {
$this->initReflection(); } $this->initReflection();
return $this->reflection; } }
public function registerExtensions(ContainerBuilder $container) { return $this->reflection;
}
public function registerExtensions(ContainerBuilder $container)
{
if (!$dir = realpath($this->getPath().'/DependencyInjection')) { if (!$dir = realpath($this->getPath().'/DependencyInjection')) {
return array(); } return;
}
$finder = new Finder(); $finder = new Finder();
$finder->files()->name('*Extension.php')->in($dir); $finder->files()->name('*Extension.php')->in($dir);
$prefix = $this->namespacePrefix.'\\'.$this->name.'\\DependencyInjection'; $prefix = $this->namespacePrefix.'\\'.$this->name.'\\DependencyInjection';
foreach ($finder as $file) { foreach ($finder as $file) {
$class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.basename($file, '.php'); $class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php');
if ('Extension' === substr($class, -9)) { $container->registerExtension(new $class());
$container->registerExtension(new $class()); } } } }
public function registerCommands(Application $application) { }
public function registerCommands(Application $application)
{
if (!$dir = realpath($this->getPath().'/Command')) { if (!$dir = realpath($this->getPath().'/Command')) {
return; } return;
}
$finder = new Finder(); $finder = new Finder();
$finder->files()->name('*Command.php')->in($dir); $finder->files()->name('*Command.php')->in($dir);
$prefix = $this->namespacePrefix.'\\'.$this->name.'\\Command'; $prefix = $this->namespacePrefix.'\\'.$this->name.'\\Command';
foreach ($finder as $file) { 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()) { if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) {
$application->add($r->newInstance()); } } } $application->add($r->newInstance());
protected function initReflection() { }
}
}
protected function initReflection()
{
$tmp = dirname(str_replace('\\', '/', get_class($this))); $tmp = dirname(str_replace('\\', '/', get_class($this)));
$this->namespacePrefix = str_replace('/', '\\', dirname($tmp)); $this->namespacePrefix = str_replace('/', '\\', dirname($tmp));
$this->name = basename($tmp); $this->name = basename($tmp);
$this->reflection = new \ReflectionObject($this); $this->reflection = new \ReflectionObject($this);
$this->path = dirname($this->reflection->getFilename()); } } $this->path = str_replace('\\', '/', dirname($this->reflection->getFilename()));
}
}
namespace Symfony\Component\HttpKernel\Bundle; namespace Symfony\Component\HttpKernel\Bundle;
interface BundleInterface { interface BundleInterface
{
function boot(); function boot();
function shutdown(); } function shutdown();
}
namespace Symfony\Component\HttpKernel\Debug; namespace Symfony\Component\HttpKernel\Debug;
class ErrorHandler { class ErrorHandler
{
protected $levels = array( protected $levels = array(
E_WARNING => 'Warning', E_WARNING => 'Warning',
E_NOTICE => 'Notice', E_NOTICE => 'Notice',
@ -70,140 +101,230 @@ class ErrorHandler {
E_RECOVERABLE_ERROR => 'Catchable Fatal Error', E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
); );
protected $level; protected $level;
public function __construct($level = null) { public function __construct($level = null)
$this->level = null === $level ? error_reporting() : $level; } {
public function register() { $this->level = null === $level ? error_reporting() : $level;
set_error_handler(array($this, 'handle')); } }
public function handle($level, $message, $file, $line, $context) { public function register()
{
set_error_handler(array($this, 'handle'));
}
public function handle($level, $message, $file, $line, $context)
{
if (0 === $this->level) { if (0 === $this->level) {
return false; } return false;
}
if (error_reporting() & $level && $this->level & $level) { 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)); } throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line));
return false; } } }
return false;
}
}
namespace Symfony\Component\HttpKernel; namespace Symfony\Component\HttpKernel;
class ClassCollectionLoader { class ClassCollectionLoader
{
static protected $loaded; 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])) { if (isset(self::$loaded[$name])) {
return; } return;
}
self::$loaded[$name] = true; self::$loaded[$name] = true;
$classes = array_unique($classes); $classes = array_unique($classes);
if ($adaptive) { if ($adaptive) {
$classes = array_diff($classes, get_declared_classes(), get_declared_interfaces()); $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'; $cache = $cacheDir.'/'.$name.'.php';
$reload = false; $reload = false;
if ($autoReload) { if ($autoReload) {
$metadata = $cacheDir.'/'.$name.'.meta'; $metadata = $cacheDir.'/'.$name.'.meta';
if (!file_exists($metadata) || !file_exists($cache)) { if (!file_exists($metadata) || !file_exists($cache)) {
$reload = true; } else { $reload = true;
} else {
$time = filemtime($cache); $time = filemtime($cache);
$meta = unserialize(file_get_contents($metadata)); $meta = unserialize(file_get_contents($metadata));
if ($meta[1] != $classes) { if ($meta[1] != $classes) {
$reload = true; } else { $reload = true;
} else {
foreach ($meta[0] as $resource) { foreach ($meta[0] as $resource) {
if (!file_exists($resource) || filemtime($resource) > $time) { if (!file_exists($resource) || filemtime($resource) > $time) {
$reload = true; $reload = true;
break; } } } } } break;
}
}
}
}
}
if (!$reload && file_exists($cache)) { if (!$reload && file_exists($cache)) {
require_once $cache; require_once $cache;
return; } return;
}
$files = array(); $files = array();
$content = ''; $content = '';
foreach ($classes as $class) { foreach ($classes as $class) {
if (!class_exists($class) && !interface_exists($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); $r = new \ReflectionClass($class);
$files[] = $r->getFileName(); $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))) { if (!is_dir(dirname($cache))) {
mkdir(dirname($cache), 0777, true); } mkdir(dirname($cache), 0777, true);
}
self::writeCacheFile($cache, Kernel::stripComments('<?php '.$content)); self::writeCacheFile($cache, Kernel::stripComments('<?php '.$content));
if ($autoReload) { if ($autoReload) {
self::writeCacheFile($metadata, serialize(array($files, $classes))); } } self::writeCacheFile($metadata, serialize(array($files, $classes)));
static protected function writeCacheFile($file, $content) { }
}
static protected function writeCacheFile($file, $content)
{
$tmpFile = tempnam(dirname($file), basename($file)); $tmpFile = tempnam(dirname($file), basename($file));
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) { if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
chmod($file, 0644); chmod($file, 0644);
return; } return;
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file)); } } }
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
}
}
namespace Symfony\Component\DependencyInjection; namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
class Container implements ContainerInterface { class Container implements ContainerInterface
{
protected $parameterBag; protected $parameterBag;
protected $services; protected $services;
public function __construct(ParameterBagInterface $parameterBag = null) { public function __construct(ParameterBagInterface $parameterBag = null)
{
$this->parameterBag = null === $parameterBag ? new ParameterBag() : $parameterBag; $this->parameterBag = null === $parameterBag ? new ParameterBag() : $parameterBag;
$this->services = array(); $this->services = array();
$this->set('service_container', $this); } $this->set('service_container', $this);
public function freeze() { }
public function freeze()
{
$this->parameterBag->resolve(); $this->parameterBag->resolve();
$this->parameterBag = new FrozenParameterBag($this->parameterBag->all()); } $this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
public function isFrozen() { }
return $this->parameterBag instanceof FrozenParameterBag; } public function isFrozen()
public function getParameterBag() { {
return $this->parameterBag; } return $this->parameterBag instanceof FrozenParameterBag;
public function getParameter($name) { }
return $this->parameterBag->get($name); } public function getParameterBag()
public function hasParameter($name) { {
return $this->parameterBag->has($name); } return $this->parameterBag;
public function setParameter($name, $value) { }
$this->parameterBag->set($name, $value); } public function getParameter($name)
public function set($id, $service) { {
$this->services[$id] = $service; } return $this->parameterBag->get($name);
public function has($id) { }
return isset($this->services[$id]) || method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service'); } public function hasParameter($name)
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) { {
$id = (string) $id; 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])) { 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')) { 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) { if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $id)); } } throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $id));
public function getServiceIds() { }
}
public function getServiceIds()
{
$ids = array(); $ids = array();
$r = new \ReflectionClass($this); $r = new \ReflectionClass($this);
foreach ($r->getMethods() as $method) { foreach ($r->getMethods() as $method) {
if (preg_match('/^get(.+)Service$/', $name = $method->getName(), $match)) { if (preg_match('/^get(.+)Service$/', $name = $method->getName(), $match)) {
$ids[] = self::underscore($match[1]); } } $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); } return array_merge($ids, array_keys($this->services));
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, '_', '.'))); } } 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; namespace Symfony\Component\DependencyInjection;
interface ContainerAwareInterface { interface ContainerAwareInterface
function setContainer(ContainerInterface $container = null); } {
function setContainer(ContainerInterface $container = null);
}
namespace Symfony\Component\DependencyInjection; namespace Symfony\Component\DependencyInjection;
interface ContainerInterface { interface ContainerInterface
{
const EXCEPTION_ON_INVALID_REFERENCE = 1; const EXCEPTION_ON_INVALID_REFERENCE = 1;
const NULL_ON_INVALID_REFERENCE = 2; const NULL_ON_INVALID_REFERENCE = 2;
const IGNORE_ON_INVALID_REFERENCE = 3; const IGNORE_ON_INVALID_REFERENCE = 3;
function set($id, $service); function set($id, $service);
function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE); function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);
function has($id); } function has($id);
}
namespace Symfony\Component\DependencyInjection\ParameterBag; namespace Symfony\Component\DependencyInjection\ParameterBag;
class FrozenParameterBag extends ParameterBag { class FrozenParameterBag extends ParameterBag
public function __construct(array $parameters = array()) { {
public function __construct(array $parameters = array())
{
foreach ($parameters as $key => $value) { foreach ($parameters as $key => $value) {
$this->parameters[strtolower($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) { public function clear()
throw new \LogicException('Impossible to call add() on a frozen ParameterBag.'); } {
public function set($name, $value) { throw new \LogicException('Impossible to call clear() on a frozen ParameterBag.');
throw new \LogicException('Impossible to call set() 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; namespace Symfony\Component\DependencyInjection\ParameterBag;
interface ParameterBagInterface { interface ParameterBagInterface
{
function clear(); function clear();
function add(array $parameters); function add(array $parameters);
function all(); function all();
function get($name); function get($name);
function set($name, $value); function set($name, $value);
function has($name); } function has($name);
}
namespace Symfony\Component\DependencyInjection; namespace Symfony\Component\DependencyInjection;
interface TaggedContainerInterface extends ContainerInterface { interface TaggedContainerInterface extends ContainerInterface
function findTaggedServiceIds($name); } {
function findTaggedServiceIds($name);
}