Skip to content

Commit

Permalink
Remove static madness
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateu Aguiló Bosch committed Jun 17, 2015
1 parent 31fd712 commit afbddea
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 74 deletions.
52 changes: 26 additions & 26 deletions src/AutoloaderBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@
*/
class AutoloaderBootstrap {

const AUTOLOAD_FUNCTION = '\Drupal\Composer\ClassLoader\Loader::autoload';
const AUTOLOAD_METHOD = 'autoload';
const COMPOSER_CONFIGURATION_NAME = 'composer.json';

/**
* Holds the composer autoloader.
*
* @var \Composer\Autoload\ClassLoader
*/
protected $classLoader;

/**
* Holds the class loader.
*
* @var \Drupal\Composer\ClassLoader\Loader
*/
protected $loader;

/**
Expand All @@ -36,42 +43,44 @@ class AutoloaderBootstrap {
/**
* Constructs a AutoloaderBootstrap object.
*
* @param \Composer\Autoload\ClassLoader $loader
* @param \Composer\Autoload\ClassLoader $classLoader
* The Composer class loader.
* @param string $seed
* The seed to find the drupal projects.
* @param Loader $loader
* The loader object to use. NULL to auto-create one.
*/
public function __construct(\Composer\Autoload\ClassLoader $loader, $seed = 'composer.json') {
$this->loader = $loader;
public function __construct(\Composer\Autoload\ClassLoader $classLoader, $seed = 'composer.json', Loader $loader = NULL) {
$this->classLoader = $classLoader;
$this->seed = $seed;
$this->loader = $loader ?: new Loader($seed);
}

/**
* Register the autoloader if it is not registered.
*/
public function register() {
if ($this::checkLoadedAutoloader()) {
if ($this->checkLoadedAutoloader()) {
return;
}
// Parse the composer.json.
$composer_config = json_decode(file_get_contents(static::COMPOSER_CONFIGURATION_NAME));
Loader::setSeed($this->seed);
$this->registerDrupalPaths($composer_config);
$this->registerPsr($composer_config);
}

/**
* Registers the autoloader.
*/
protected static function load() {
spl_autoload_register(static::AUTOLOAD_FUNCTION);
protected function load() {
spl_autoload_register(array($this->loader, static::AUTOLOAD_METHOD));
}

/**
* Unregisters the autoloader.
*/
protected static function unload() {
spl_autoload_unregister(static::AUTOLOAD_FUNCTION);
protected function unload() {
spl_autoload_unregister(array($this->loader, static::AUTOLOAD_METHOD));
}

/**
Expand All @@ -84,8 +93,8 @@ protected function registerDrupalPaths($composer_config) {
if (empty($composer_config->{'class-loader'}->{'drupal-path'})) {
return;
}
Loader::setClassMap((array) $composer_config->{'class-loader'}->{'drupal-path'});
$this::load();
$this->loader->setClassMap((array) $composer_config->{'class-loader'}->{'drupal-path'});
$this->load();
}

/**
Expand All @@ -105,30 +114,21 @@ protected function registerPsr($composer_config) {
if (empty($psr4) && empty($psr0)) {
return;
}
Loader::setPsrClassMap(array(
$this->loader->setPsrClassMap(array(
'psr-0' => $psr0,
'psr-4' => $psr4,
));
Loader::registerPsr($this->loader);
$this->loader->registerPsr($this->classLoader);
}

/**
* Checks if the autoloader has been added.
*
* @return bool
*/
public static function checkLoadedAutoloader() {
$autoloader_str = ltrim(static::AUTOLOAD_FUNCTION, '\\');
$autoloader = explode('::', $autoloader_str);
foreach (spl_autoload_functions() as $callable) {
if (
($callable[0] == $autoloader[0] || $callable[0] == $autoloader[0]) &&
$callable[1] == $autoloader[1]
) {
return TRUE;
}
}
return FALSE;
public function checkLoadedAutoloader() {
$functions = spl_autoload_functions();
return in_array(array($this->loader, static::AUTOLOAD_METHOD), $functions);
}

}
46 changes: 28 additions & 18 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Loader implements LoaderInterface {
*
* @var array
*/
protected static $classMap;
protected $classMap;

/**
* PSR Class maps.
Expand All @@ -29,7 +29,7 @@ class Loader implements LoaderInterface {
*
* @var array
*/
protected static $psrClassMap;
protected $psrClassMap;

/**
* Seed.
Expand All @@ -38,31 +38,41 @@ class Loader implements LoaderInterface {
*
* @var
*/
protected static $seed;
protected $seed;

/**
* Constructs a Loader object.
*
* @param string $seed.
* This is the path of the composer.json file that triggered the bootstrap.
*/
public function __construct($seed) {
$this->seed = $seed;
}

/**
* {@inheritdoc}
*/
public static function autoload($class) {
return static::autoloadPaths($class);
public function autoload($class) {
return $this->autoloadPaths($class);
}

/**
* {@inheritdoc}
*/
public static function setClassMap(array $class_map) {
public function setClassMap(array $class_map) {
// Remove the leading \ from the class names.
$unprefixed_class_map = array();
foreach ($class_map as $class_name => $tokenized_path) {
$unprefixed_class_map[static::unprefixClass($class_name)] = $tokenized_path;
}
static::$classMap = $unprefixed_class_map;
$this->classMap = $unprefixed_class_map;
}

/**
* {@inheritdoc}
*/
public static function setPsrClassMap(array $class_map) {
public function setPsrClassMap(array $class_map) {
// Remove the leading \ from the partial namespaces.
$unprefixed_class_map = array();
foreach ($class_map as $psr => $psr_class_map) {
Expand All @@ -71,14 +81,14 @@ public static function setPsrClassMap(array $class_map) {
$unprefixed_class_map[$psr][static::unprefixClass($class_name)] = $tokenized_path;
}
}
static::$psrClassMap = $unprefixed_class_map;
$this->psrClassMap = $unprefixed_class_map;
}

/**
* {@inheritdoc}
*/
public static function setSeed($seed) {
static::$seed = $seed;
public function setSeed($seed) {
$this->seed = $seed;
}

/**
Expand Down Expand Up @@ -106,19 +116,19 @@ protected static function unprefixClass($class) {
* @return bool
* TRUE if the class was found. FALSE otherwise.
*/
protected static function autoloadPaths($class) {
protected function autoloadPaths($class) {
$class = static::unprefixClass($class);
// If the class that PHP is trying to find is not in the class map, built
// from the composer configuration, then bail.
if (!in_array($class, array_keys(static::$classMap))) {
if (!in_array($class, array_keys($this->classMap))) {
return FALSE;
}
try {
$resolver = new TokenResolver(static::$classMap[$class]);
$resolver = new TokenResolver($this->classMap[$class]);
$finder = $resolver->resolve();
// Have the path finder require the file and return TRUE or FALSE if it
// found the file or not.
$finder->requireFile(static::$seed);
$finder->requireFile($this->seed);
return TRUE;
}
catch (ClassLoaderException $e) {
Expand All @@ -130,7 +140,7 @@ protected static function autoloadPaths($class) {
/**
* {@inheritdoc}
*/
public static function registerPsr(\Composer\Autoload\ClassLoader $loader) {
public function registerPsr(\Composer\Autoload\ClassLoader $loader) {
// Composer's autoloader uses a different method to add each PSR partial
// namespace.
$psrs = array(
Expand All @@ -148,7 +158,7 @@ public static function registerPsr(\Composer\Autoload\ClassLoader $loader) {
// [ 'Drupal\\plug\\' => ['DRUPAL_CONTRIB<plug>/lib', …] ],
// ],
// ]
foreach (static::$psrClassMap[$psr] as $partial_namespace => $tokenized_paths) {
foreach ($this->psrClassMap[$psr] as $partial_namespace => $tokenized_paths) {
if (!is_array($tokenized_paths)) {
// If a string was passed, then convert it to an array for
// consistency.
Expand All @@ -160,7 +170,7 @@ public static function registerPsr(\Composer\Autoload\ClassLoader $loader) {
$resolver = new TokenResolver($tokenized_path);
$finder = $resolver->resolve();
// Get the real path of the prefix.
$real_path = $finder->find(static::$seed);
$real_path = $finder->find($this->seed);
$loader->{$loader_method}($partial_namespace, $real_path);
}
catch (ClassLoaderException $e) {}
Expand Down
10 changes: 5 additions & 5 deletions src/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ interface LoaderInterface {
* @return bool
* TRUE if the class is currently available, FALSE otherwise.
*/
public static function autoload($class);
public function autoload($class);

/**
* Sets the class map.
*
* @param array $class_map
*/
public static function setClassMap(array $class_map);
public function setClassMap(array $class_map);

/**
* Sets the PSR class map.
*
* @param array[] $class_map
*/
public static function setPsrClassMap(array $class_map);
public function setPsrClassMap(array $class_map);

/**
* Sets the seed path.
*
* @param string $seed.
*/
public static function setSeed($seed);
public function setSeed($seed);

/**
* Helper function to register PSR-0 and PSR-4 based files.
Expand All @@ -51,6 +51,6 @@ public static function setSeed($seed);
* @return bool
* TRUE if the class was found. FALSE otherwise.
*/
public static function registerPsr(\Composer\Autoload\ClassLoader $loader);
public function registerPsr(\Composer\Autoload\ClassLoader $loader);

}
6 changes: 3 additions & 3 deletions tests/src/AutoloaderBootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AutoloaderBootstrapTest extends \PHPUnit_Framework_TestCase {
public function test___construct() {
$loader = m::mock('\Composer\Autoload\ClassLoader');
$autoloader = new AutoloaderBootstrap($loader);
$reflection_property = new \ReflectionProperty(get_class($autoloader), 'loader');
$reflection_property = new \ReflectionProperty(get_class($autoloader), 'classLoader');
$reflection_property->setAccessible(TRUE);
$value = $reflection_property->getValue($autoloader);
$this->assertEquals($loader, $value);
Expand All @@ -47,10 +47,10 @@ public function test_register() {
$autoloader = new AutoloaderBootstrap($loader, 'data/docroot/sites/all/modules/testmodule/composer.json');
$autoloader->register();

$this->assertTrue($autoloader::checkLoadedAutoloader());
$this->assertTrue($autoloader->checkLoadedAutoloader());
// Make sure that calling to register a second time does not fail.
$autoloader->register();
$this->assertTrue($autoloader::checkLoadedAutoloader());
$this->assertTrue($autoloader->checkLoadedAutoloader());
}


Expand Down

0 comments on commit afbddea

Please sign in to comment.