Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[ClassLoader] A PSR-4 compatible class loader#10100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
derrabus wants to merge5 commits intosymfony:masterfromderrabus:psr-4
Closed
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
[ClassLoader] A PSR-4 compatible class loader.
  • Loading branch information
@derrabus
derrabus committedJan 21, 2014
commit1d7d5cae8ef7c27d09b906d569ef2d8710496155
94 changes: 94 additions & 0 deletionssrc/Symfony/Component/ClassLoader/Psr4ClassLoader.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
<?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-4 class loader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

we used to add a dot after the short description

*
* See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I agree. We should change this inSymfony\Component\ClassLoader\ClassLoader as well, then.

*
* @author Alexander M. Turek <me@derrabus.de>
*/
class Psr4ClassLoader
{
/**
* @var array
*/
private $prefixes = array();

/**
* @param string $prefix
* @param string $baseDir
*/
public function addPrefix($prefix, $baseDir)
{
$prefix = trim($prefix, '\\') . '\\';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

we don't put spaces around the. operator

$baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$this->prefixes[] = array($prefix, $baseDir);
}

/**
* @param string $class
* @return string|null
*/
public function findFile($class)
{
$class = ltrim($class, '\\');

foreach ($this->prefixes as $current) {
list($currentPrefix, $currentBaseDir) = $current;
if (strpos($class, $currentPrefix) === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

if (0 === strpos($class, $currentPrefix)) {

$classWithoutPrefix = substr($class, strlen($currentPrefix));
$file = $currentBaseDir . str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutPrefix) . '.php';
if (file_exists($file)) {
return $file;
}
}
}

return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

return;

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is this standard or personal taste? I prefer explicitly writingreturn null; if my method is not meant to be void.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

it's a standard

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

IMOreturn null is more correct, as before you have otherreturn $file;, so IDE etc. will for sure complain about inconsensity with justreturn;.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

imo, when discussion what is correct, we should not consider what IDEs will do. And actually, this hole return statement should be removed. It does not make sense at all.

}

/**
* @param string $class
* @return bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

add an empty comment line between@param and@return

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Symfony CS uses Boolean (with the first letter uppercase)

*/
public function loadClass($class)
{
$file = $this->findFile($class);
if ($file !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

if (null !== $file) {

require $file;

return true;
}

return false;
}

/**
* Registers this instance as an autoloader.
*
* @param bool $prepend
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}

/**
* Removes this instance from the registered autoloaders.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
<?php

namespace Acme\DemoLib;

class Class_With_Underscores
{
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
<?php

namespace Acme\DemoLib;

class Foo
{
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
<?php

namespace Acme\DemoLib\Lets\Go\Deeper;

class Class_With_Underscores
{
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
<?php

namespace Acme\DemoLib\Lets\Go\Deeper;

class Foo
{
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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\ClassLoader\Tests;

use Symfony\Component\ClassLoader\Psr4ClassLoader;

class Psr4ClassLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @param string $className
* @dataProvider getLoadClassTests
*/
public function testLoadClass($className)
{
$loader = new Psr4ClassLoader();
$loader->addPrefix(
'Acme\\DemoLib',
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'psr-4'
);
$loader->loadClass($className);
$this->assertTrue(class_exists($className), sprintf('loadClass() should load %s', $className));
}

/**
* @return array
*/
public function getLoadClassTests()
{
return array(
array('Acme\\DemoLib\\Foo'),
array('Acme\\DemoLib\\Class_With_Underscores'),
array('Acme\\DemoLib\\Lets\\Go\\Deeper\\Foo'),
array('Acme\\DemoLib\\Lets\\Go\\Deeper\\Class_With_Underscores')
);
}

/**
* @param string $className
* @dataProvider getLoadNonexistentClassTests
*/
public function testLoadNonexistentClass($className)
{
$loader = new Psr4ClassLoader();
$loader->addPrefix(
'Acme\\DemoLib',
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'psr-4'
);
$loader->loadClass($className);
$this->assertFalse(class_exists($className), sprintf('loadClass() should not load %s', $className));
}

/**
* @return array
*/
public function getLoadNonexistentClassTests()
{
return array(
array('Acme\\DemoLib\\I_Do_Not_Exist'),
array('UnknownVendor\\SomeLib\\I_Do_Not_Exist')
);
}
}

[8]ページ先頭

©2009-2025 Movatter.jp