Forked from docteurklein/UniversalClassLoaderChecker.php
Created
April 28, 2011 07:51
-
-
Save chanmix51/945988 to your computer and use it in GitHub Desktop.
a class to check classLoader configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Symfony\Component\ClassLoader\UniversalClassLoader; | |
$loader = new UniversalClassLoader(); | |
<?php | |
$loader->registerNamespaces(array( | |
'Symfony' => array(__DIR__.'/../a/non/existent/path/to/symfony/src', __DIR__.'/../vendor/bundles'), | |
'Sensio' => __DIR__.'/../vendor/bundles', | |
'JMS' => __DIR__.'/../vendor/bundles', | |
)); | |
$loader->registerPrefixes(array( | |
'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib', | |
'Twig_' => __DIR__.'/../vendor/twig/lib/bad/path/too', | |
'Swift_' => __DIR__.'/../vendor/swiftmailer/lib/classes', | |
)); | |
$checker = new Symfony\Component\ClassLoader\UniversalClassLoaderChecker; | |
var_dump($checker->check($loader)); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// will output: | |
array(2) { | |
[0]=> | |
string(134) "registered namespace "Symfony" does not match with path "/var/local/webroot/helios2/app/../a/non/existent/path/to/symfony/src/Symfony"" | |
[1]=> | |
string(121) "registered prefix "Twig_" does not match with path "/var/local/webroot/helios2/app/../vendor/twig/lib/bad/path/too/Twig/"" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Symfony\Component\ClassLoader; | |
class UniversalClassLoaderChecker | |
{ | |
public function check(UniversalClassLoader $classLoader) | |
{ | |
return array_merge( | |
$this->checkNamespaces($classLoader), | |
$this->checkPrefixes($classLoader) | |
); | |
} | |
public function checkNamespaces(UniversalClassLoader $classLoader) | |
{ | |
$errors = array(); | |
foreach ($classLoader->getNamespaces() as $namespace => $paths) { | |
$paths = (array)$paths; | |
foreach($paths as $path) { | |
$fullPath = sprintf('%s%s%s', $path, DIRECTORY_SEPARATOR, str_replace('\\', DIRECTORY_SEPARATOR, $namespace)); | |
if (false === is_dir($fullPath)) { | |
$errors[] = sprintf('registered namespace "%s" does not match with path "%s"', $namespace, $fullPath); | |
} | |
} | |
} | |
return $errors; | |
} | |
public function checkPrefixes(UniversalClassLoader $classLoader) | |
{ | |
$errors = array(); | |
foreach ($classLoader->getPrefixes() as $prefix => $paths) { | |
$paths = (array)$paths; | |
foreach($paths as $path) { | |
$fullPath = sprintf('%s%s%s', $path, DIRECTORY_SEPARATOR, str_replace('_', DIRECTORY_SEPARATOR, $prefix)); | |
if (false === is_dir($fullPath)) { | |
$errors[] = sprintf('registered prefix "%s" does not match with path "%s"', $prefix, $fullPath); | |
} | |
} | |
} | |
return $errors; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment