Compare commits

..

4 Commits
v2.3.3 ... 2.2

Author SHA1 Message Date
Fabien Potencier
0a5217edb6 fixed @expectedException class names 2013-11-25 09:44:14 +01:00
bronze1man
77f69969b1 Fix some annotates 2013-09-19 11:36:05 +02:00
Fabien Potencier
8478872572 updated the composer install command to reflect changes in Composer 2013-09-18 09:27:26 +02:00
Lee Rowlands
827c54ee98 Use strstr instead of strpos 2013-08-09 09:16:43 +02:00
14 changed files with 37 additions and 154 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
vendor/
composer.lock
phpunit.xml

View File

@@ -1,11 +1,6 @@
CHANGELOG
=========
2.3.0
-----
* added a WinCacheClassLoader for WinCache
2.1.0
-----

View File

@@ -63,7 +63,7 @@ class ClassCollectionLoader
// auto-reload
$reload = false;
if ($autoReload) {
$metadata = $cache.'.meta';
$metadata = $cacheDir.'/'.$name.$extension.'.meta';
if (!is_file($metadata) || !is_file($cache)) {
$reload = true;
} else {

View File

@@ -164,9 +164,13 @@ class ClassLoader
*/
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;
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
$className = substr($class, $pos + 1);
} else {
// PEAR-like class name
@@ -174,21 +178,21 @@ class ClassLoader
$className = $class;
}
$classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
$classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
foreach ($this->prefixes as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
if ($class === strstr($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
return $dir.DIRECTORY_SEPARATOR.$classPath;
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 (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
}

View File

@@ -39,7 +39,7 @@ class ClassMapGenerator
/**
* Iterate over all files in the given directory searching for classes
*
* @param Iterator|string $dir The directory to search in or an iterator
* @param \Iterator|string $dir The directory to search in or an iterator
*
* @return array A class map array
*/
@@ -121,7 +121,7 @@ class ClassMapGenerator
}
}
$classes[] = ltrim($namespace.$class, '\\');
$classes[] = ltrim($namespace . $class, '\\');
break;
default:
break;

View File

@@ -47,6 +47,10 @@ class MapClassLoader
*/
public function loadClass($class)
{
if ('\\' === $class[0]) {
$class = substr($class, 1);
}
if (isset($this->map[$class])) {
require $this->map[$class];
}
@@ -61,6 +65,10 @@ class MapClassLoader
*/
public function findFile($class)
{
if ('\\' === $class[0]) {
$class = substr($class, 1);
}
if (isset($this->map[$class])) {
return $this->map[$class];
}

View File

@@ -65,5 +65,5 @@ Resources
You can run the unit tests with the following command:
$ cd path/to/Symfony/Component/ClassLoader/
$ composer.phar install --dev
$ composer.phar install
$ phpunit

View File

@@ -189,7 +189,7 @@ class ClassCollectionLoaderTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException InvalidArgumentException
* @expectedException \InvalidArgumentException
*/
public function testUnableToLoadClassException()
{

View File

@@ -54,6 +54,8 @@ class ClassLoaderTest extends \PHPUnit_Framework_TestCase
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'),
);
}
@@ -98,7 +100,7 @@ class ClassLoaderTest extends \PHPUnit_Framework_TestCase
$loader->setUseIncludePath(true);
$this->assertTrue($loader->getUseIncludePath());
set_include_path(__DIR__.'/Fixtures/includepath'.PATH_SEPARATOR.$includePath);
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'));

View File

@@ -125,7 +125,7 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
}
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->in(__DIR__.'/Fixtures/beta/NamespaceCollision');
$finder->files()->in(__DIR__ . '/Fixtures/beta/NamespaceCollision');
$this->assertEqualsNormalized(array(
'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',

View File

@@ -32,6 +32,8 @@ class UniversalClassLoaderTest extends \PHPUnit_Framework_TestCase
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'),
);
}
@@ -47,7 +49,7 @@ class UniversalClassLoaderTest extends \PHPUnit_Framework_TestCase
$loader->useIncludePath(true);
$this->assertTrue($loader->getUseIncludePath());
set_include_path(__DIR__.'/Fixtures/includepath'.PATH_SEPARATOR.$includePath);
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'));

View File

@@ -263,6 +263,10 @@ class UniversalClassLoader
*/
public function findFile($class)
{
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$namespace = substr($class, 0, $pos);

View File

@@ -1,133 +0,0 @@
<?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;
/**
* WinCacheClassLoader implements a wrapping autoloader cached in WinCache.
*
* 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 WinCacheClassLoader('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 Artem Ryzhkov <artem@smart-core.org>
*/
class WinCacheClassLoader
{
private $prefix;
/**
* The class loader object being decorated.
*
* @var \Symfony\Component\ClassLoader\ClassLoader
* A class loader object that implements the findFile() method.
*/
protected $decorated;
/**
* Constructor.
*
* @param string $prefix The WinCache namespace prefix to use.
* @param object $decorated A class loader object that implements the findFile() method.
*
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function __construct($prefix, $decorated)
{
if (!extension_loaded('wincache')) {
throw new \RuntimeException('Unable to use WinCacheClassLoader as WinCache is not enabled.');
}
if (!method_exists($decorated, 'findFile')) {
throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
}
$this->prefix = $prefix;
$this->decorated = $decorated;
}
/**
* 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 WinCache.
*
* @param string $class A class name to resolve to file
*
* @return string|null
*/
public function findFile($class)
{
if (false === $file = wincache_ucache_get($this->prefix.$class)) {
wincache_ucache_set($this->prefix.$class, $file = $this->decorated->findFile($class), 0);
}
return $file;
}
/**
* Passes through all unknown calls onto the decorated object.
*/
public function __call($method, $args)
{
return call_user_func_array(array($this->decorated, $method), $args);
}
}

View File

@@ -28,7 +28,7 @@
"target-dir": "Symfony/Component/ClassLoader",
"extra": {
"branch-alias": {
"dev-master": "2.3-dev"
"dev-master": "2.2-dev"
}
}
}