- Patch #1542710 by BTMash, Crell: update to latest Symfony 2.1 code.

8.0.x
Dries 2012-04-28 18:07:40 -04:00
parent 4c24974c24
commit 34ef955ae5
410 changed files with 24950 additions and 551 deletions

View File

@ -0,0 +1,117 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader;
/**
* ApcClassLoader implements a wrapping autoloader cached in APC for PHP 5.3.
*
* It expects an object implementing a findFile method to find the file. This
* allow using it as a wrapper around the other loaders of the component (the
* ClassLoader and the UniversalClassLoader for instance) but also around any
* other autoloader following this convention (the Composer one for instance)
*
* $loader = new ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* $cachedLoader = new ApcClassLoader('my_prefix', $loader);
*
* // activate the cached autoloader
* $cachedLoader->register();
*
* // eventually deactivate the non-cached loader if it was registered previously
* // to be sure to use the cached one.
* $loader->unregister();
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Kris Wallsmith <kris@symfony.com>
*
* @api
*/
class ApcClassLoader
{
private $prefix;
private $classFinder;
/**
* Constructor.
*
* @param string $prefix A prefix to create a namespace in APC
* @param object $classFinder
*
* @api
*/
public function __construct($prefix, $classFinder)
{
if (!extension_loaded('apc')) {
throw new \RuntimeException('Unable to use ApcUniversalClassLoader as APC is not enabled.');
}
if (!method_exists($classFinder, 'findFile')) {
throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
}
$this->prefix = $prefix;
$this->classFinder = $classFinder;
}
/**
* Registers this instance as an autoloader.
*
* @param Boolean $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return Boolean|null True, if loaded
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
require $file;
return true;
}
}
/**
* Finds a file by class name while caching lookups to APC.
*
* @param string $class A class name to resolve to file
*
* @return string|null
*/
public function findFile($class)
{
if (false === $file = apc_fetch($this->prefix.$class)) {
apc_store($this->prefix.$class, $file = $this->classFinder->findFile($class));
}
return $file;
}
}

View File

@ -181,7 +181,7 @@ class ClassCollectionLoader
{ {
$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, 0666 & ~umask());
return; return;
} }

View File

@ -0,0 +1,187 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader;
/**
* ClassLoader implements an PSR-0 class loader
*
* See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
*
* $loader = new ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* // activate the autoloader
* $loader->register();
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->setUseIncludePath(true);
*
* In this example, if you try to use a class in the Symfony\Component
* namespace or one of its children (Symfony\Component\Console for instance),
* the autoloader will first look for the class under the component/
* directory, and it will then fallback to the framework/ directory if not
* found before giving up.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ClassLoader
{
private $prefixes = array();
private $fallbackDirs = array();
private $useIncludePath = false;
public function getPrefixes()
{
return $this->prefixes;
}
public function getFallbackDirs()
{
return $this->fallbackDirs;
}
public function addPrefixes(array $prefixes)
{
foreach ($prefixes as $prefix => $path) {
$this->addPrefix($prefix, $path);
}
}
/**
* Registers a set of classes
*
* @param string $prefix The classes prefix
* @param array|string $paths The location(s) of the classes
*/
public function addPrefix($prefix, $paths)
{
if (!$prefix) {
foreach ((array) $paths as $path) {
$this->fallbackDirs[] = $path;
}
return;
}
if (isset($this->prefixes[$prefix])) {
$this->prefixes[$prefix] = array_merge(
$this->prefixes[$prefix],
(array) $paths
);
} else {
$this->prefixes[$prefix] = (array) $paths;
}
}
/**
* Turns on searching the include for class files.
*
* @param Boolean $useIncludePath
*/
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return Boolean
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Registers this instance as an autoloader.
*
* @param Boolean $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return Boolean|null True, if loaded
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
require $file;
return true;
}
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|null The path, if found
*/
public function findFile($class)
{
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
$className = substr($class, $pos + 1);
} else {
// PEAR-like class name
$classPath = null;
$className = $class;
}
$classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
foreach ($this->prefixes as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
}
}
}
foreach ($this->fallbackDirs as $dir) {
if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
}
if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) {
return $file;
}
}
}

View File

@ -0,0 +1,90 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader;
/**
* Autoloader checking if the class is really defined in the file found.
*
* The DebugClassLoader will wrap all registered autoloaders providing a
* findFile method and will throw an exception if a file is found but does
* not declare the class.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Christophe Coevoet <stof@notk.org>
*
* @api
*/
class DebugClassLoader
{
private $classFinder;
/**
* Constructor.
*
* @param object $classFinder
*
* @api
*/
public function __construct($classFinder)
{
$this->classFinder = $classFinder;
}
/**
* Replaces all autoloaders implementing a findFile method by a DebugClassLoader wrapper.
*/
static public function enable()
{
if (!is_array($functions = spl_autoload_functions())) {
return;
}
foreach ($functions as $function) {
spl_autoload_unregister($function);
}
foreach ($functions as $function) {
if (is_array($function) && method_exists($function[0], 'findFile')) {
$function = array(new static($function[0]), 'loadClass');
}
spl_autoload_register($function);
}
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return Boolean|null True, if loaded
*/
public function loadClass($class)
{
if ($file = $this->classFinder->findFile($class)) {
require $file;
if (!class_exists($class, false) && !interface_exists($class, false) && (!function_exists('trait_exists') || !trait_exists($class, false))) {
throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
}
return true;
}
}
}

View File

@ -58,3 +58,16 @@ class loader:
Furthermore, the component provides tools to aggregate classes into a single Furthermore, the component provides tools to aggregate classes into a single
file, which is especially useful to improve performance on servers that do not file, which is especially useful to improve performance on servers that do not
provide byte caches. provide byte caches.
Resources
---------
You can run the unit tests with the following command:
phpunit -c src/Symfony/Component/ClassLoader/
If you also want to run the unit tests that depend on other Symfony
Components, declare the following environment variables before running
PHPUnit:
export SYMFONY_FINDER=../path/to/Finder

View File

@ -0,0 +1,192 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader\Tests;
use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
class ApcUniversalClassLoaderTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!extension_loaded('apc')) {
$this->markTestSkipped('The apc extension is not available.');
}
if (!(ini_get('apc.enabled') && ini_get('apc.enable_cli'))) {
$this->markTestSkipped('The apc extension is available, but not enabled.');
} else {
apc_clear_cache('user');
}
}
protected function tearDown()
{
if (ini_get('apc.enabled') && ini_get('apc.enable_cli')) {
apc_clear_cache('user');
}
}
public function testConstructor()
{
$loader = new ApcUniversalClassLoader('test.prefix.');
$loader->registerNamespace('Apc\Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$this->assertEquals($loader->findFile('\Apc\Namespaced\FooBar'), apc_fetch('test.prefix.\Apc\Namespaced\FooBar'), '__construct() takes a prefix as its first argument');
}
/**
* @dataProvider getLoadClassTests
*/
public function testLoadClass($className, $testClassName, $message)
{
$loader = new ApcUniversalClassLoader('test.prefix.');
$loader->registerNamespace('Apc\Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->registerPrefix('Apc_Pearlike_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->loadClass($testClassName);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassTests()
{
return array(
array('\\Apc\\Namespaced\\Foo', '\\Apc\\Namespaced\\Foo', '->loadClass() loads Apc\Namespaced\Foo class'),
array('Apc_Pearlike_Foo', 'Apc_Pearlike_Foo', '->loadClass() loads Apc_Pearlike_Foo class'),
array('\\Apc\\Namespaced\\Bar', '\\Apc\\Namespaced\\Bar', '->loadClass() loads Apc\Namespaced\Bar class with a leading slash'),
array('Apc_Pearlike_Bar', '\\Apc_Pearlike_Bar', '->loadClass() loads Apc_Pearlike_Bar class with a leading slash'),
);
}
/**
* @dataProvider getLoadClassFromFallbackTests
*/
public function testLoadClassFromFallback($className, $testClassName, $message)
{
$loader = new ApcUniversalClassLoader('test.prefix.fallback');
$loader->registerNamespace('Apc\Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->registerPrefix('Apc_Pearlike_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->registerNamespaceFallbacks(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/fallback'));
$loader->registerPrefixFallbacks(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/fallback'));
$loader->loadClass($testClassName);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassFromFallbackTests()
{
return array(
array('\\Apc\\Namespaced\\Baz', '\\Apc\\Namespaced\\Baz', '->loadClass() loads Apc\Namespaced\Baz class'),
array('Apc_Pearlike_Baz', 'Apc_Pearlike_Baz', '->loadClass() loads Apc_Pearlike_Baz class'),
array('\\Apc\\Namespaced\\FooBar', '\\Apc\\Namespaced\\FooBar', '->loadClass() loads Apc\Namespaced\Baz class from fallback dir'),
array('Apc_Pearlike_FooBar', 'Apc_Pearlike_FooBar', '->loadClass() loads Apc_Pearlike_Baz class from fallback dir'),
);
}
/**
* @dataProvider getLoadClassNamespaceCollisionTests
*/
public function testLoadClassNamespaceCollision($namespaces, $className, $message)
{
$loader = new ApcUniversalClassLoader('test.prefix.collision.');
$loader->registerNamespaces($namespaces);
$loader->loadClass($className);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassNamespaceCollisionTests()
{
return array(
array(
array(
'Apc\\NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
'Apc\\NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
),
'\Apc\NamespaceCollision\A\Foo',
'->loadClass() loads NamespaceCollision\A\Foo from alpha.',
),
array(
array(
'Apc\\NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
'Apc\\NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
),
'\Apc\NamespaceCollision\A\Bar',
'->loadClass() loads NamespaceCollision\A\Bar from alpha.',
),
array(
array(
'Apc\\NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
'Apc\\NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
),
'\Apc\NamespaceCollision\A\B\Foo',
'->loadClass() loads NamespaceCollision\A\B\Foo from beta.',
),
array(
array(
'Apc\\NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
'Apc\\NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
),
'\Apc\NamespaceCollision\A\B\Bar',
'->loadClass() loads NamespaceCollision\A\B\Bar from beta.',
),
);
}
/**
* @dataProvider getLoadClassPrefixCollisionTests
*/
public function testLoadClassPrefixCollision($prefixes, $className, $message)
{
$loader = new ApcUniversalClassLoader('test.prefix.collision.');
$loader->registerPrefixes($prefixes);
$loader->loadClass($className);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassPrefixCollisionTests()
{
return array(
array(
array(
'ApcPrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
'ApcPrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
),
'ApcPrefixCollision_A_Foo',
'->loadClass() loads ApcPrefixCollision_A_Foo from alpha.',
),
array(
array(
'ApcPrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
'ApcPrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
),
'ApcPrefixCollision_A_Bar',
'->loadClass() loads ApcPrefixCollision_A_Bar from alpha.',
),
array(
array(
'ApcPrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
'ApcPrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
),
'ApcPrefixCollision_A_B_Foo',
'->loadClass() loads ApcPrefixCollision_A_B_Foo from beta.',
),
array(
array(
'ApcPrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
'ApcPrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
),
'ApcPrefixCollision_A_B_Bar',
'->loadClass() loads ApcPrefixCollision_A_B_Bar from beta.',
),
);
}
}

View File

@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader\Tests;
use Symfony\Component\ClassLoader\ClassCollectionLoader;
class ClassCollectionLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testFixNamespaceDeclarations()
{
$source = <<<EOF
<?php
namespace Foo;
class Foo {}
namespace Bar ;
class Foo {}
namespace Foo\Bar;
class Foo {}
namespace Foo\Bar\Bar
{
class Foo {}
}
namespace
{
class Foo {}
}
EOF;
$expected = <<<EOF
<?php
namespace Foo
{
class Foo {}
}
namespace Bar
{
class Foo {}
}
namespace Foo\Bar
{
class Foo {}
}
namespace Foo\Bar\Bar
{
class Foo {}
}
namespace
{
class Foo {}
}
EOF;
$this->assertEquals($expected, ClassCollectionLoader::fixNamespaceDeclarations($source));
}
/**
* @expectedException InvalidArgumentException
*/
public function testUnableToLoadClassException()
{
ClassCollectionLoader::load(array('SomeNotExistingClass'), '', 'foo', false);
}
}

View File

@ -0,0 +1,163 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader\Tests;
use Symfony\Component\ClassLoader\ClassLoader;
class ClassLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getLoadClassTests
*/
public function testLoadClass($className, $testClassName, $message)
{
$loader = new ClassLoader();
$loader->addPrefix('Namespaced2\\', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->addPrefix('Pearlike2_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->loadClass($testClassName);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassTests()
{
return array(
array('\\Namespaced2\\Foo', 'Namespaced2\\Foo', '->loadClass() loads Namespaced2\Foo class'),
array('\\Pearlike2_Foo', 'Pearlike2_Foo', '->loadClass() loads Pearlike2_Foo class'),
array('\\Namespaced2\\Bar', '\\Namespaced2\\Bar', '->loadClass() loads Namespaced2\Bar class with a leading slash'),
array('\\Pearlike2_Bar', '\\Pearlike2_Bar', '->loadClass() loads Pearlike2_Bar class with a leading slash'),
);
}
public function testUseIncludePath()
{
$loader = new ClassLoader();
$this->assertFalse($loader->getUseIncludePath());
$this->assertNull($loader->findFile('Foo'));
$includePath = get_include_path();
$loader->setUseIncludePath(true);
$this->assertTrue($loader->getUseIncludePath());
set_include_path(__DIR__.'/Fixtures/includepath' . PATH_SEPARATOR . $includePath);
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'includepath'.DIRECTORY_SEPARATOR.'Foo.php', $loader->findFile('Foo'));
set_include_path($includePath);
}
/**
* @dataProvider getLoadClassFromFallbackTests
*/
public function testLoadClassFromFallback($className, $testClassName, $message)
{
$loader = new ClassLoader();
$loader->addPrefix('Namespaced2\\', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->addPrefix('Pearlike2_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->addPrefix('', array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/fallback'));
$loader->loadClass($testClassName);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassFromFallbackTests()
{
return array(
array('\\Namespaced2\\Baz', 'Namespaced2\\Baz', '->loadClass() loads Namespaced2\Baz class'),
array('\\Pearlike2_Baz', 'Pearlike2_Baz', '->loadClass() loads Pearlike2_Baz class'),
array('\\Namespaced2\\FooBar', 'Namespaced2\\FooBar', '->loadClass() loads Namespaced2\Baz class from fallback dir'),
array('\\Pearlike2_FooBar', 'Pearlike2_FooBar', '->loadClass() loads Pearlike2_Baz class from fallback dir'),
);
}
/**
* @dataProvider getLoadClassNamespaceCollisionTests
*/
public function testLoadClassNamespaceCollision($namespaces, $className, $message)
{
$loader = new ClassLoader();
$loader->addPrefixes($namespaces);
$loader->loadClass($className);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassNamespaceCollisionTests()
{
return array(
array(
array(
'NamespaceCollision\\C' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
'NamespaceCollision\\C\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
),
'NamespaceCollision\C\Foo',
'->loadClass() loads NamespaceCollision\C\Foo from alpha.',
),
array(
array(
'NamespaceCollision\\C\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
'NamespaceCollision\\C' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
),
'NamespaceCollision\C\Bar',
'->loadClass() loads NamespaceCollision\C\Bar from alpha.',
),
array(
array(
'NamespaceCollision\\C' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
'NamespaceCollision\\C\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
),
'NamespaceCollision\C\B\Foo',
'->loadClass() loads NamespaceCollision\C\B\Foo from beta.',
),
array(
array(
'NamespaceCollision\\C\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
'NamespaceCollision\\C' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
),
'NamespaceCollision\C\B\Bar',
'->loadClass() loads NamespaceCollision\C\B\Bar from beta.',
),
array(
array(
'PrefixCollision_C_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
'PrefixCollision_C_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
),
'PrefixCollision_C_Foo',
'->loadClass() loads PrefixCollision_C_Foo from alpha.',
),
array(
array(
'PrefixCollision_C_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
'PrefixCollision_C_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
),
'PrefixCollision_C_Bar',
'->loadClass() loads PrefixCollision_C_Bar from alpha.',
),
array(
array(
'PrefixCollision_C_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
'PrefixCollision_C_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
),
'PrefixCollision_C_B_Foo',
'->loadClass() loads PrefixCollision_C_B_Foo from beta.',
),
array(
array(
'PrefixCollision_C_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
'PrefixCollision_C_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
),
'PrefixCollision_C_B_Bar',
'->loadClass() loads PrefixCollision_C_B_Bar from beta.',
),
);
}
}

View File

@ -0,0 +1,102 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader\Tests;
use Symfony\Component\ClassLoader\ClassMapGenerator;
class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getTestCreateMapTests
*/
public function testCreateMap($directory, $expected)
{
$this->assertEqualsNormalized($expected, ClassMapGenerator::createMap($directory));
}
public function getTestCreateMapTests()
{
$data = array(
array(__DIR__.'/Fixtures/Namespaced', array(
'Namespaced\\Bar' => realpath(__DIR__).'/Fixtures/Namespaced/Bar.php',
'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php',
'Namespaced\\Baz' => realpath(__DIR__).'/Fixtures/Namespaced/Baz.php',
)
),
array(__DIR__.'/Fixtures/beta/NamespaceCollision', array(
'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
'NamespaceCollision\\A\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Foo.php',
'NamespaceCollision\\C\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/C/B/Bar.php',
'NamespaceCollision\\C\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/C/B/Foo.php',
)),
array(__DIR__.'/Fixtures/Pearlike', array(
'Pearlike_Foo' => realpath(__DIR__).'/Fixtures/Pearlike/Foo.php',
'Pearlike_Bar' => realpath(__DIR__).'/Fixtures/Pearlike/Bar.php',
'Pearlike_Baz' => realpath(__DIR__).'/Fixtures/Pearlike/Baz.php',
)),
array(__DIR__.'/Fixtures/classmap', array(
'Foo\\Bar\\A' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
'Foo\\Bar\\B' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
'A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
'Alpha\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
'Alpha\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
'Beta\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
'Beta\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
'ClassMap\\SomeInterface' => realpath(__DIR__).'/Fixtures/classmap/SomeInterface.php',
'ClassMap\\SomeParent' => realpath(__DIR__).'/Fixtures/classmap/SomeParent.php',
'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php',
)),
);
if (version_compare(PHP_VERSION, '5.4', '>=')) {
$data[] = array(__DIR__.'/Fixtures/php5.4', array(
'TFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
'CFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
'Foo\\TBar' => __DIR__.'/Fixtures/php5.4/traits.php',
'Foo\\IBar' => __DIR__.'/Fixtures/php5.4/traits.php',
'Foo\\TFooBar' => __DIR__.'/Fixtures/php5.4/traits.php',
'Foo\\CBar' => __DIR__.'/Fixtures/php5.4/traits.php',
));
}
return $data;
}
public function testCreateMapFinderSupport()
{
if (!class_exists('Symfony\\Component\\Finder\\Finder')) {
$this->markTestSkipped('Finder component is not available');
}
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->in(__DIR__ . '/Fixtures/beta/NamespaceCollision');
$this->assertEqualsNormalized(array(
'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
'NamespaceCollision\\A\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Foo.php',
'NamespaceCollision\\C\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/C/B/Bar.php',
'NamespaceCollision\\C\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/C/B/Foo.php',
), ClassMapGenerator::createMap($finder));
}
protected function assertEqualsNormalized($expected, $actual, $message = null)
{
foreach ($expected as $ns => $path) {
$expected[$ns] = strtr($path, '\\', '/');
}
foreach ($actual as $ns => $path) {
$actual[$ns] = strtr($path, '\\', '/');
}
$this->assertEquals($expected, $actual, $message);
}
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\Namespaced;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\Namespaced;
class Baz
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\Namespaced;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\Namespaced;
class FooBar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Apc_Pearlike_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Apc_Pearlike_Baz
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Apc_Pearlike_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class ApcPrefixCollision_A_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class ApcPrefixCollision_A_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\NamespaceCollision\A;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\NamespaceCollision\A;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class ApcPrefixCollision_A_B_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class ApcPrefixCollision_A_B_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\NamespaceCollision\A\B;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\NamespaceCollision\A\B;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Apc_Pearlike_FooBar
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Apc\Namespaced;
class FooBar
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Namespaced;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Namespaced;
class Baz
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Namespaced;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,8 @@
<?php
namespace Namespaced2;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,8 @@
<?php
namespace Namespaced2;
class Baz
{
public static $loaded = true;
}

View File

@ -0,0 +1,8 @@
<?php
namespace Namespaced2;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Pearlike_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Pearlike_Baz
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Pearlike_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Pearlike2_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Pearlike2_Baz
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Pearlike2_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NamespaceCollision\A;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NamespaceCollision\A;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,8 @@
<?php
namespace NamespaceCollision\C;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,8 @@
<?php
namespace NamespaceCollision\C;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class PrefixCollision_A_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class PrefixCollision_A_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class PrefixCollision_C_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class PrefixCollision_C_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NamespaceCollision\A\B;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NamespaceCollision\A\B;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,8 @@
<?php
namespace NamespaceCollision\C\B;
class Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,8 @@
<?php
namespace NamespaceCollision\C\B;
class Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class PrefixCollision_A_B_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class PrefixCollision_A_B_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class PrefixCollision_C_B_Bar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class PrefixCollision_C_B_Foo
{
public static $loaded = true;
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ClassMap;
class SomeClass extends SomeParent implements SomeInterface
{
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ClassMap;
interface SomeInterface
{
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ClassMap;
abstract class SomeParent
{
}

View File

@ -0,0 +1,14 @@
<?php
namespace {
class A {}
}
namespace Alpha {
class A {}
class B {}
}
namespace Beta {
class A {}
class B {}
}

View File

@ -0,0 +1,3 @@
<?php
$a = new stdClass();

View File

@ -0,0 +1 @@
This file should be skipped.

View File

@ -0,0 +1,15 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Foo\Bar;
class A {}
class B {}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Namespaced;
class FooBar
{
public static $loaded = true;
}

View File

@ -0,0 +1,8 @@
<?php
namespace Namespaced2;
class FooBar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Pearlike_FooBar
{
public static $loaded = true;
}

View File

@ -0,0 +1,6 @@
<?php
class Pearlike2_FooBar
{
public static $loaded = true;
}

View File

@ -0,0 +1,5 @@
<?php
class Foo
{
}

View File

@ -0,0 +1,196 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader\Tests;
use Symfony\Component\ClassLoader\UniversalClassLoader;
class UniversalClassLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getLoadClassTests
*/
public function testLoadClass($className, $testClassName, $message)
{
$loader = new UniversalClassLoader();
$loader->registerNamespace('Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->registerPrefix('Pearlike_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->loadClass($testClassName);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassTests()
{
return array(
array('\\Namespaced\\Foo', 'Namespaced\\Foo', '->loadClass() loads Namespaced\Foo class'),
array('\\Pearlike_Foo', 'Pearlike_Foo', '->loadClass() loads Pearlike_Foo class'),
array('\\Namespaced\\Bar', '\\Namespaced\\Bar', '->loadClass() loads Namespaced\Bar class with a leading slash'),
array('\\Pearlike_Bar', '\\Pearlike_Bar', '->loadClass() loads Pearlike_Bar class with a leading slash'),
);
}
public function testUseIncludePath()
{
$loader = new UniversalClassLoader();
$this->assertFalse($loader->getUseIncludePath());
$this->assertNull($loader->findFile('Foo'));
$includePath = get_include_path();
$loader->useIncludePath(true);
$this->assertTrue($loader->getUseIncludePath());
set_include_path(__DIR__.'/Fixtures/includepath' . PATH_SEPARATOR . $includePath);
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'includepath'.DIRECTORY_SEPARATOR.'Foo.php', $loader->findFile('Foo'));
set_include_path($includePath);
}
/**
* @dataProvider getLoadClassFromFallbackTests
*/
public function testLoadClassFromFallback($className, $testClassName, $message)
{
$loader = new UniversalClassLoader();
$loader->registerNamespace('Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->registerPrefix('Pearlike_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
$loader->registerNamespaceFallbacks(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/fallback'));
$loader->registerPrefixFallbacks(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/fallback'));
$loader->loadClass($testClassName);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassFromFallbackTests()
{
return array(
array('\\Namespaced\\Baz', 'Namespaced\\Baz', '->loadClass() loads Namespaced\Baz class'),
array('\\Pearlike_Baz', 'Pearlike_Baz', '->loadClass() loads Pearlike_Baz class'),
array('\\Namespaced\\FooBar', 'Namespaced\\FooBar', '->loadClass() loads Namespaced\Baz class from fallback dir'),
array('\\Pearlike_FooBar', 'Pearlike_FooBar', '->loadClass() loads Pearlike_Baz class from fallback dir'),
);
}
public function testRegisterPrefixFallback()
{
$loader = new UniversalClassLoader();
$loader->registerPrefixFallback(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/fallback');
$this->assertEquals(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/fallback'), $loader->getPrefixFallbacks());
}
public function testRegisterNamespaceFallback()
{
$loader = new UniversalClassLoader();
$loader->registerNamespaceFallback(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/Namespaced/fallback');
$this->assertEquals(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/Namespaced/fallback'), $loader->getNamespaceFallbacks());
}
/**
* @dataProvider getLoadClassNamespaceCollisionTests
*/
public function testLoadClassNamespaceCollision($namespaces, $className, $message)
{
$loader = new UniversalClassLoader();
$loader->registerNamespaces($namespaces);
$loader->loadClass($className);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassNamespaceCollisionTests()
{
return array(
array(
array(
'NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
'NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
),
'NamespaceCollision\A\Foo',
'->loadClass() loads NamespaceCollision\A\Foo from alpha.',
),
array(
array(
'NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
'NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
),
'NamespaceCollision\A\Bar',
'->loadClass() loads NamespaceCollision\A\Bar from alpha.',
),
array(
array(
'NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
'NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
),
'NamespaceCollision\A\B\Foo',
'->loadClass() loads NamespaceCollision\A\B\Foo from beta.',
),
array(
array(
'NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
'NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
),
'NamespaceCollision\A\B\Bar',
'->loadClass() loads NamespaceCollision\A\B\Bar from beta.',
),
);
}
/**
* @dataProvider getLoadClassPrefixCollisionTests
*/
public function testLoadClassPrefixCollision($prefixes, $className, $message)
{
$loader = new UniversalClassLoader();
$loader->registerPrefixes($prefixes);
$loader->loadClass($className);
$this->assertTrue(class_exists($className), $message);
}
public function getLoadClassPrefixCollisionTests()
{
return array(
array(
array(
'PrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
'PrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
),
'PrefixCollision_A_Foo',
'->loadClass() loads PrefixCollision_A_Foo from alpha.',
),
array(
array(
'PrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
'PrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
),
'PrefixCollision_A_Bar',
'->loadClass() loads PrefixCollision_A_Bar from alpha.',
),
array(
array(
'PrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
'PrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
),
'PrefixCollision_A_B_Foo',
'->loadClass() loads PrefixCollision_A_B_Foo from beta.',
),
array(
array(
'PrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
'PrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
),
'PrefixCollision_A_B_Bar',
'->loadClass() loads PrefixCollision_A_B_Bar from beta.',
),
);
}
}

View File

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
spl_autoload_register(function ($class) {
foreach (array(
'SYMFONY_FINDER' => 'Finder',
) as $env => $name) {
if (isset($_SERVER[$env]) && 0 === strpos(ltrim($class, '/'), 'Symfony\Component\\'.$name)) {
if (file_exists($file = $_SERVER[$env].'/'.substr(str_replace('\\', '/', $class), strlen('Symfony\Component\\'.$name)).'.php')) {
require_once $file;
}
}
}
if (0 === strpos(ltrim($class, '/'), 'Symfony\Component\ClassLoader')) {
if (file_exists($file = __DIR__.'/../'.substr(str_replace('\\', '/', $class), strlen('Symfony\Component\ClassLoader')).'.php')) {
require_once $file;
}
}
});

View File

@ -0,0 +1,119 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\ClassLoader;
/**
* XcacheClassLoader implements a wrapping autoloader cached in Xcache for PHP 5.3.
*
* It expects an object implementing a findFile method to find the file. This
* allows using it as a wrapper around the other loaders of the component (the
* ClassLoader and the UniversalClassLoader for instance) but also around any
* other autoloader following this convention (the Composer one for instance)
*
* $loader = new ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* $cachedLoader = new XcacheClassLoader('my_prefix', $loader);
*
* // activate the cached autoloader
* $cachedLoader->register();
*
* // eventually deactivate the non-cached loader if it was registered previously
* // to be sure to use the cached one.
* $loader->unregister();
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Kris Wallsmith <kris@symfony.com>
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*
* @api
*/
class XcacheClassLoader
{
private $prefix;
private $classFinder;
/**
* Constructor.
*
* @param string $prefix A prefix to create a namespace in Xcache
* @param object $classFinder
*
* @api
*/
public function __construct($prefix, $classFinder)
{
if (!extension_loaded('Xcache')) {
throw new \RuntimeException('Unable to use XcacheClassLoader as Xcache is not enabled.');
}
if (!method_exists($classFinder, 'findFile')) {
throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
}
$this->prefix = $prefix;
$this->classFinder = $classFinder;
}
/**
* Registers this instance as an autoloader.
*
* @param Boolean $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return Boolean|null True, if loaded
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
require $file;
return true;
}
}
/**
* Finds a file by class name while caching lookups to Xcache.
*
* @param string $class A class name to resolve to file
*
* @return string|null
*/
public function findFile($class)
{
if (xcache_isset($this->prefix.$class)) {
$file = xcache_get($this->prefix.$class);
} else {
xcache_set($this->prefix.$class, $file = $this->classFinder->findFile($class));
}
return $file;
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="Tests/bootstrap.php"
>
<testsuites>
<testsuite name="Symfony ClassLoader Component Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Compiler; namespace Symfony\Component\DependencyInjection\Compiler;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Compiler; namespace Symfony\Component\DependencyInjection\Compiler;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Compiler; namespace Symfony\Component\DependencyInjection\Compiler;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Compiler; namespace Symfony\Component\DependencyInjection\Compiler;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Compiler; namespace Symfony\Component\DependencyInjection\Compiler;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Compiler; namespace Symfony\Component\DependencyInjection\Compiler;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Compiler; namespace Symfony\Component\DependencyInjection\Compiler;

View File

@ -59,7 +59,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
* *
* @api * @api
*/ */
class Container implements ContainerInterface class Container implements IntrospectableContainerInterface
{ {
protected $parameterBag; protected $parameterBag;
protected $services; protected $services;
@ -266,6 +266,18 @@ class Container implements ContainerInterface
} }
} }
/**
* Returns true if the given service has actually been initialized
*
* @param string $id The service identifier
*
* @return Boolean true if service has already been initialized, false otherwise
*/
public function initialized($id)
{
return isset($this->services[strtolower($id)]);
}
/** /**
* Gets all service ids. * Gets all service ids.
* *

View File

@ -162,7 +162,7 @@ class Definition
} }
/** /**
* Sets the service class. * Gets the service class.
* *
* @return string The service class * @return string The service class
* *
@ -451,6 +451,22 @@ class Definition
return isset($this->tags[$name]); return isset($this->tags[$name]);
} }
/**
* Clears all tags for a given name.
*
* @param string $name The tag name
*
* @return Definition
*/
public function clearTag($name)
{
if (isset($this->tags[$name])) {
unset($this->tags[$name]);
}
return $this;
}
/** /**
* Clears the tags for this definition. * Clears the tags for this definition.
* *

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection; namespace Symfony\Component\DependencyInjection;

View File

@ -21,7 +21,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
* XmlDumper dumps a service container as an XML string. * XmlDumper dumps a service container as an XML string.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* @author Martin Haso? <martin.hason@gmail.com> * @author Martin Hasoň <martin.hason@gmail.com>
* *
* @api * @api
*/ */

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Exception; namespace Symfony\Component\DependencyInjection\Exception;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Exception; namespace Symfony\Component\DependencyInjection\Exception;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Exception; namespace Symfony\Component\DependencyInjection\Exception;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Exception; namespace Symfony\Component\DependencyInjection\Exception;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Exception; namespace Symfony\Component\DependencyInjection\Exception;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Exception; namespace Symfony\Component\DependencyInjection\Exception;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Exception; namespace Symfony\Component\DependencyInjection\Exception;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection\Exception; namespace Symfony\Component\DependencyInjection\Exception;

View File

@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection;
/**
* IntrospectableContainerInterface defines additional introspection functionality
* for containers, allowing logic to be implemented based on a Container's state.
*
* @author Evan Villemez <evillemez@gmail.com>
*
*/
interface IntrospectableContainerInterface extends ContainerInterface
{
/**
* Check for whether or not a service has been initialized.
*
* @param string $id
*
* @return Boolean true if the service has been initialized, false otherwise
*
*/
function initialized($id);
}

View File

@ -193,7 +193,8 @@ class YamlFileLoader extends FileLoader
if (isset($service['calls'])) { if (isset($service['calls'])) {
foreach ($service['calls'] as $call) { foreach ($service['calls'] as $call) {
$definition->addMethodCall($call[0], $this->resolveServices($call[1])); $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
$definition->addMethodCall($call[0], $args);
} }
} }
@ -210,6 +211,12 @@ class YamlFileLoader extends FileLoader
$name = $tag['name']; $name = $tag['name'];
unset($tag['name']); unset($tag['name']);
foreach ($tag as $attribute => $value) {
if (!is_scalar($value)) {
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s" in %s.', $id, $name, $file));
}
}
$definition->addTag($name, $tag); $definition->addTag($name, $tag);
} }
} }

View File

@ -18,9 +18,61 @@ Here is a simple example that shows how to register services and parameters:
$sc->get('foo'); $sc->get('foo');
Method Calls (Setter Injection):
$sc = new ContainerBuilder();
$sc
->register('bar', '%bar.class%')
->addMethodCall('setFoo', array(new Reference('foo')))
;
$sc->setParameter('bar.class', 'Bar');
$sc->get('bar');
Factory Class:
If your service is retrieved by calling a static method:
$sc = new ContainerBuilder();
$sc
->register('bar', '%bar.class%')
->setFactoryClass('%bar.class%')
->setFactoryMethod('getInstance')
->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');
$sc->get('bar');
File Include:
For some services, especially those that are difficult or impossible to
autoload, you may need the container to include a file before
instantiating your class.
$sc = new ContainerBuilder();
$sc
->register('bar', '%bar.class%')
->setFile('/path/to/file')
->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');
$sc->get('bar');
Resources Resources
--------- ---------
Unit tests: You can run the unit tests with the following command:
https://github.com/symfony/symfony/tree/master/tests/Symfony/Tests/Component/DependencyInjection phpunit -c src/Symfony/Component/DependencyInjection/
If you also want to run the unit tests that depend on other Symfony
Components, declare the following environment variables before running
PHPUnit:
export SYMFONY_CONFIG=../path/to/Config
export SYMFONY_YAML=../path/to/Yaml

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection; namespace Symfony\Component\DependencyInjection;

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony package.
* *
* (c) Fabien Potencier <fabien@symfony.com> * (c) Fabien Potencier <fabien@symfony.com>
* *
* This source file is subject to the MIT license that is bundled * For the full copyright and license information, please view the LICENSE
* with this source code in the file LICENSE. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\DependencyInjection; namespace Symfony\Component\DependencyInjection;

View File

@ -0,0 +1,100 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AnalyzeServiceReferencesPassTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$a = $container
->register('a')
->addArgument($ref1 = new Reference('b'))
;
$b = $container
->register('b')
->addMethodCall('setA', array($ref2 = new Reference('a')))
;
$c = $container
->register('c')
->addArgument($ref3 = new Reference('a'))
->addArgument($ref4 = new Reference('b'))
;
$d = $container
->register('d')
->setProperty('foo', $ref5 = new Reference('b'))
;
$graph = $this->process($container);
$this->assertCount(3, $edges = $graph->getNode('b')->getInEdges());
$this->assertSame($ref1, $edges[0]->getValue());
$this->assertSame($ref4, $edges[1]->getValue());
$this->assertSame($ref5, $edges[2]->getValue());
}
public function testProcessDetectsReferencesFromInlinedDefinitions()
{
$container = new ContainerBuilder();
$container
->register('a')
;
$container
->register('b')
->addArgument(new Definition(null, array($ref = new Reference('a'))))
;
$graph = $this->process($container);
$this->assertCount(1, $refs = $graph->getNode('a')->getInEdges());
$this->assertSame($ref, $refs[0]->getValue());
}
public function testProcessDoesNotSaveDuplicateReferences()
{
$container = new ContainerBuilder();
$container
->register('a')
;
$container
->register('b')
->addArgument(new Definition(null, array($ref1 = new Reference('a'))))
->addArgument(new Definition(null, array($ref2 = new Reference('a'))))
;
$graph = $this->process($container);
$this->assertCount(2, $graph->getNode('a')->getInEdges());
}
protected function process(ContainerBuilder $container)
{
$pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));
$pass->process($container);
return $container->getCompiler()->getServiceReferenceGraph();
}
}

View File

@ -0,0 +1,85 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CheckCircularReferencesPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
*/
public function testProcess()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->register('b')->addArgument(new Reference('a'));
$this->process($container);
}
/**
* @expectedException \RuntimeException
*/
public function testProcessWithAliases()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->setAlias('b', 'c');
$container->setAlias('c', 'a');
$this->process($container);
}
/**
* @expectedException \RuntimeException
*/
public function testProcessDetectsIndirectCircularReference()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->register('b')->addArgument(new Reference('c'));
$container->register('c')->addArgument(new Reference('a'));
$this->process($container);
}
public function testProcessIgnoresMethodCalls()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->register('b')->addMethodCall('setA', array(new Reference('a')));
$this->process($container);
}
protected function process(ContainerBuilder $container)
{
$compiler = new Compiler();
$passConfig = $compiler->getPassConfig();
$passConfig->setOptimizationPasses(array(
new AnalyzeServiceReferencesPass(true),
new CheckCircularReferencesPass(),
));
$passConfig->setRemovingPasses(array());
$compiler->compile($container);
}
}

View File

@ -0,0 +1,69 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CheckDefinitionValidityPass;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CheckDefinitionValidityPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
*/
public function testProcessDetectsSyntheticNonPublicDefinitions()
{
$container = new ContainerBuilder();
$container->register('a')->setSynthetic(true)->setPublic(false);
$this->process($container);
}
/**
* @expectedException \RuntimeException
*/
public function testProcessDetectsSyntheticPrototypeDefinitions()
{
$container = new ContainerBuilder();
$container->register('a')->setSynthetic(true)->setScope(ContainerInterface::SCOPE_PROTOTYPE);
$this->process($container);
}
/**
* @expectedException \RuntimeException
*/
public function testProcessDetectsNonSyntheticNonAbstractDefinitionWithoutClass()
{
$container = new ContainerBuilder();
$container->register('a')->setSynthetic(false)->setAbstract(false);
$this->process($container);
}
public function testProcess()
{
$container = new ContainerBuilder();
$container->register('a', 'class');
$container->register('b', 'class')->setSynthetic(true)->setPublic(true);
$container->register('c', 'class')->setAbstract(true);
$container->register('d', 'class')->setSynthetic(true);
$this->process($container);
}
protected function process(ContainerBuilder $container)
{
$pass = new CheckDefinitionValidityPass();
$pass->process($container);
}
}

View File

@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CheckExceptionOnInvalidReferenceBehaviorPassTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$container
->register('a', '\stdClass')
->addArgument(new Reference('b'))
;
$container->register('b', '\stdClass');
}
/**
* @expectedException Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/
public function testProcessThrowsExceptionOnInvalidReference()
{
$container = new ContainerBuilder();
$container
->register('a', '\stdClass')
->addArgument(new Reference('b'))
;
$this->process($container);
}
/**
* @expectedException Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/
public function testProcessThrowsExceptionOnInvalidReferenceFromInlinedDefinition()
{
$container = new ContainerBuilder();
$def = new Definition();
$def->addArgument(new Reference('b'));
$container
->register('a', '\stdClass')
->addArgument($def)
;
$this->process($container);
}
private function process(ContainerBuilder $container)
{
$pass = new CheckExceptionOnInvalidReferenceBehaviorPass();
$pass->process($container);
}
}

View File

@ -0,0 +1,98 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use Symfony\Component\DependencyInjection\Scope;
use Symfony\Component\DependencyInjection\Compiler\CheckReferenceValidityPass;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CheckReferenceValidityPassTest extends \PHPUnit_Framework_TestCase
{
public function testProcessIgnoresScopeWideningIfNonStrictReference()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
$container->register('b')->setScope('prototype');
$this->process($container);
}
/**
* @expectedException \RuntimeException
*/
public function testProcessDetectsScopeWidening()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->register('b')->setScope('prototype');
$this->process($container);
}
public function testProcessIgnoresCrossScopeHierarchyReferenceIfNotStrict()
{
$container = new ContainerBuilder();
$container->addScope(new Scope('a'));
$container->addScope(new Scope('b'));
$container->register('a')->setScope('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
$container->register('b')->setScope('b');
$this->process($container);
}
/**
* @expectedException \RuntimeException
*/
public function testProcessDetectsCrossScopeHierarchyReference()
{
$container = new ContainerBuilder();
$container->addScope(new Scope('a'));
$container->addScope(new Scope('b'));
$container->register('a')->setScope('a')->addArgument(new Reference('b'));
$container->register('b')->setScope('b');
$this->process($container);
}
/**
* @expectedException \RuntimeException
*/
public function testProcessDetectsReferenceToAbstractDefinition()
{
$container = new ContainerBuilder();
$container->register('a')->setAbstract(true);
$container->register('b')->addArgument(new Reference('a'));
$this->process($container);
}
public function testProcess()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->register('b');
$this->process($container);
}
protected function process(ContainerBuilder $container)
{
$pass = new CheckReferenceValidityPass();
$pass->process($container);
}
}

View File

@ -0,0 +1,134 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use Symfony\Component\DependencyInjection\Scope;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class InlineServiceDefinitionsPassTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$container
->register('inlinable.service')
->setPublic(false)
;
$container
->register('service')
->setArguments(array(new Reference('inlinable.service')))
;
$this->process($container);
$arguments = $container->getDefinition('service')->getArguments();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $arguments[0]);
$this->assertSame($container->getDefinition('inlinable.service'), $arguments[0]);
}
public function testProcessDoesNotInlineWhenAliasedServiceIsNotOfPrototypeScope()
{
$container = new ContainerBuilder();
$container
->register('foo')
->setPublic(false)
;
$container->setAlias('moo', 'foo');
$container
->register('service')
->setArguments(array($ref = new Reference('foo')))
;
$this->process($container);
$arguments = $container->getDefinition('service')->getArguments();
$this->assertSame($ref, $arguments[0]);
}
public function testProcessDoesInlineServiceOfPrototypeScope()
{
$container = new ContainerBuilder();
$container
->register('foo')
->setScope('prototype')
;
$container
->register('bar')
->setPublic(false)
->setScope('prototype')
;
$container->setAlias('moo', 'bar');
$container
->register('service')
->setArguments(array(new Reference('foo'), $ref = new Reference('moo'), new Reference('bar')))
;
$this->process($container);
$arguments = $container->getDefinition('service')->getArguments();
$this->assertEquals($container->getDefinition('foo'), $arguments[0]);
$this->assertNotSame($container->getDefinition('foo'), $arguments[0]);
$this->assertSame($ref, $arguments[1]);
$this->assertEquals($container->getDefinition('bar'), $arguments[2]);
$this->assertNotSame($container->getDefinition('bar'), $arguments[2]);
}
public function testProcessInlinesIfMultipleReferencesButAllFromTheSameDefinition()
{
$container = new ContainerBuilder();
$a = $container->register('a')->setPublic(false);
$b = $container
->register('b')
->addArgument(new Reference('a'))
->addArgument(new Definition(null, array(new Reference('a'))))
;
$this->process($container);
$arguments = $b->getArguments();
$this->assertSame($a, $arguments[0]);
$inlinedArguments = $arguments[1]->getArguments();
$this->assertSame($a, $inlinedArguments[0]);
}
public function testProcessInlinesOnlyIfSameScope()
{
$container = new ContainerBuilder();
$container->addScope(new Scope('foo'));
$a = $container->register('a')->setPublic(false)->setScope('foo');
$b = $container->register('b')->addArgument(new Reference('a'));
$this->process($container);
$arguments = $b->getArguments();
$this->assertEquals(new Reference('a'), $arguments[0]);
$this->assertTrue($container->hasDefinition('a'));
}
protected function process(ContainerBuilder $container)
{
$repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()));
$repeatedPass->process($container);
}
}

Some files were not shown because too many files have changed in this diff Show More