[Framework] updated the stripComments() method

This commit is contained in:
Fabien Potencier 2010-08-10 18:46:37 +02:00
parent c87dd7780f
commit 0e36f043ef
2 changed files with 101 additions and 322 deletions

View File

@ -415,28 +415,37 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
return new DelegatingLoader($resolver);
}
/**
* Removes comments from a PHP source string.
*
* We don't use the PHP php_strip_whitespace() function
* as we want the content to be readable and well-formatted.
*
* @param string $source A PHP string
*
* @return string The PHP string with the comments removed
*/
static public function stripComments($source)
{
if (!function_exists('token_get_all')) {
return $source;
}
$ignore = array(T_COMMENT => true, T_DOC_COMMENT => true);
$output = '';
foreach (token_get_all($source) as $token) {
// array
if (isset($token[1])) {
// no action on comments
if (!isset($ignore[$token[0]])) {
// anything else -> output "as is"
$output .= $token[1];
}
} else {
// simple 1-character token
if (is_string($token)) {
$output .= $token;
} elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$output .= $token[1];
}
}
// replace multiple new lines with a single newline
$output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
// reformat {} "a la python"
$output = preg_replace(array('/\n\s*\{/', '/\n\s*\}/'), array(' {', ' }'), $output);
return $output;
}

View File

@ -1,242 +1,111 @@
<?php
<?php
namespace Symfony\Framework\Bundle;
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\DependencyInjection\ContainerBuilder;
use Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Components\Console\Application;
use Symfony\Components\Finder\Finder;
abstract class Bundle implements BundleInterface
{
abstract class Bundle implements BundleInterface {
protected $container;
protected $name;
protected $namespacePrefix;
protected $path;
protected $reflection;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function boot()
{
}
public function shutdown()
{
}
public function getName()
{
public function setContainer(ContainerInterface $container = null) {
$this->container = $container; }
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 array(); }
$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)
{
$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'));
if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract()) {
$application->addCommand($r->newInstance());
}
}
}
protected function initReflection()
{
$application->addCommand($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 = dirname($this->reflection->getFilename()); } }
namespace Symfony\Framework\Bundle;
use Symfony\Components\DependencyInjection\ContainerInterface;
interface BundleInterface
{
interface BundleInterface {
public function boot();
public function shutdown();
public function setContainer(ContainerInterface $container);
}
public function setContainer(ContainerInterface $container); }
namespace Symfony\Framework;
use Symfony\Framework\Bundle\Bundle;
use Symfony\Framework\ClassCollectionLoader;
class KernelBundle extends Bundle
{
public function boot()
{
class KernelBundle extends Bundle {
public function boot() {
if ($this->container->has('error_handler')) {
$this->container['error_handler'];
}
$this->container['error_handler']; }
if ($this->container->getParameterBag()->has('kernel.include_core_classes') && $this->container->getParameter('kernel.include_core_classes')) {
ClassCollectionLoader::load($this->container->getParameter('kernel.compiled_classes'), $this->container->getParameter('kernel.cache_dir'), 'classes', $this->container->getParameter('kernel.debug'));
}
}
}
ClassCollectionLoader::load($this->container->getParameter('kernel.compiled_classes'), $this->container->getParameter('kernel.cache_dir'), 'classes', $this->container->getParameter('kernel.debug')); } } }
namespace Symfony\Framework\DependencyInjection;
use Symfony\Components\DependencyInjection\Extension\Extension;
use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Components\DependencyInjection\ContainerBuilder;
class KernelExtension extends Extension
{
public function testLoad($config, ContainerBuilder $container)
{
class KernelExtension extends Extension {
public function testLoad($config, ContainerBuilder $container) {
$loader = new XmlFileLoader($container, array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
$loader->load('test.xml');
$container->setParameter('kernel.include_core_classes', false);
}
public function sessionLoad($config, ContainerBuilder $container)
{
$container->setParameter('kernel.include_core_classes', false); }
public function sessionLoad($config, ContainerBuilder $container) {
if (!$container->hasDefinition('session')) {
$loader = new XmlFileLoader($container, array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
$loader->load('session.xml');
}
$loader->load('session.xml'); }
if (isset($config['default_locale'])) {
$container->setParameter('session.default_locale', $config['default_locale']);
}
$container->setParameter('session.default_locale', $config['default_locale']); }
if (isset($config['class'])) {
$container->setParameter('session.class', $config['class']);
}
$container->setParameter('session.class', $config['class']); }
foreach (array('name', 'lifetime', 'path', 'domain', 'secure', 'httponly', 'cache_limiter', 'pdo.db_table') as $name) {
if (isset($config['session'][$name])) {
$container->setParameter('session.options.'.$name, $config['session'][$name]);
}
}
$container->setParameter('session.options.'.$name, $config['session'][$name]); } }
if (isset($config['session']['class'])) {
$class = $config['session']['class'];
if (in_array($class, array('Native', 'Pdo'))) {
$class = 'Symfony\\Components\\HttpFoundation\\SessionStorage\\'.$class.'SessionStorage';
}
$container->setParameter('session.session', 'session.session.'.strtolower($class));
}
}
public function configLoad($config, ContainerBuilder $container)
{
$class = 'Symfony\\Components\\HttpFoundation\\SessionStorage\\'.$class.'SessionStorage'; }
$container->setParameter('session.session', 'session.session.'.strtolower($class)); } }
public function configLoad($config, ContainerBuilder $container) {
if (!$container->hasDefinition('event_dispatcher')) {
$loader = new XmlFileLoader($container, array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
$loader->load('services.xml');
if ($container->getParameter('kernel.debug')) {
$loader->load('debug.xml');
$container->setDefinition('event_dispatcher', $container->findDefinition('debug.event_dispatcher'));
}
}
$container->setDefinition('event_dispatcher', $container->findDefinition('debug.event_dispatcher')); } }
if (isset($config['charset'])) {
$container->setParameter('kernel.charset', $config['charset']);
}
$container->setParameter('kernel.charset', $config['charset']); }
if (!array_key_exists('compilation', $config)) {
$classes = array(
'Symfony\\Components\\Routing\\RouterInterface',
@ -260,52 +129,25 @@ class KernelExtension extends Extension
'Symfony\\Bundle\\FrameworkBundle\\RequestListener',
'Symfony\\Bundle\\FrameworkBundle\\Controller',
'Symfony\\Bundle\\FrameworkBundle\\Templating\\Engine',
);
} else {
); } else {
$classes = array();
foreach (explode("\n", $config['compilation']) as $class) {
if ($class) {
$classes[] = trim($class);
}
}
}
$classes[] = trim($class); } } }
$container->setParameter('kernel.compiled_classes', $classes);
if (array_key_exists('error_handler', $config)) {
if (false === $config['error_handler']) {
$container->getDefinition('error_handler')->setMethodCalls(array());
} else {
$container->getDefinition('error_handler')->setMethodCalls(array()); } else {
$container->getDefinition('error_handler')->addMethodCall('register', array());
$container->setParameter('error_handler.level', $config['error_handler']);
}
}
}
public function getXsdValidationBasePath()
{
return false;
}
public function getNamespace()
{
return 'http://www.symfony-project.org/schema/dic/symfony/kernel';
}
public function getAlias()
{
return 'kernel';
}
}
$container->setParameter('error_handler.level', $config['error_handler']); } } }
public function getXsdValidationBasePath() {
return false; }
public function getNamespace() {
return 'http://www.symfony-project.org/schema/dic/symfony/kernel'; }
public function getAlias() {
return 'kernel'; } }
namespace Symfony\Framework\Debug;
class ErrorHandler
{
class ErrorHandler {
protected $levels = array(
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
@ -315,138 +157,66 @@ class ErrorHandler
E_STRICT => 'Runtime Notice',
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\Framework;
class ClassCollectionLoader
{
static public function load($classes, $cacheDir, $name, $autoReload)
{
class ClassCollectionLoader {
static public function load($classes, $cacheDir, $name, $autoReload) {
$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('<?php '.$content));
if ($autoReload) {
self::writeCacheFile($metadata, serialize(array($files, $classes)));
}
}
static protected function writeCacheFile($file, $content)
{
self::writeCacheFile($metadata, serialize(array($files, $classes))); } }
static protected function writeCacheFile($file, $content) {
$tmpFile = tempnam(dirname($file), basename($file));
if (!$fp = @fopen($tmpFile, 'wb')) {
die(sprintf('Failed to write cache file "%s".', $tmpFile));
}
die(sprintf('Failed to write cache file "%s".', $tmpFile)); }
@fwrite($fp, $content);
@fclose($fp);
if ($content != file_get_contents($tmpFile)) {
die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
}
die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile)); }
if (!@rename($tmpFile, $file)) {
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
}
chmod($file, 0644);
}
}
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file)); }
chmod($file, 0644); } }
namespace Symfony\Framework;
use Symfony\Components\EventDispatcher\EventDispatcher as BaseEventDispatcher;
use Symfony\Components\EventDispatcher\Event;
use Symfony\Components\DependencyInjection\ContainerInterface;
class EventDispatcher extends BaseEventDispatcher
{
public function __construct(ContainerInterface $container)
{
class EventDispatcher extends BaseEventDispatcher {
public function __construct(ContainerInterface $container) {
foreach ($container->findTaggedServiceIds('kernel.listener') as $id => $attributes) {
$container->get($id)->register($this);
}
}
}
$container->get($id)->register($this); } } }