Permalink
Browse files

introduced ClassMapLoader

  • Loading branch information...
1 parent 6db21a1 commit bd0523ec8e404d7ce9c8ca3c553811d99120f593 fqqdk committed Jul 16, 2010
@@ -5,6 +5,10 @@
* @author fqqdk <fqqdk@freemail.hu>
*/
+if (interface_exists('ClassLoader')) {
+ return false;
+}
+
/**
* Description of ClassLoader
*/
View
@@ -8,7 +8,7 @@
/**
* Description of ClassMap
*/
-class ClassMap implements ClassLoader
+class ClassMap
{
/**
* @var ClassListener for class related events
@@ -67,29 +67,15 @@ public function addClass($className, SplFileInfo $file)
}
/**
- * Returns an array copy of the internal map
+ * Creates a ClassLoader over the internal map
*
- * @return array
- */
- public function getArrayCopy()
- {
- return $this->map->getArrayCopy();
- }
-
- /**
- * Attempts to load a class
+ * @param string $baseDir optional basedir to prefix the files with
*
- * @param string $className the name of the class to load
- *
- * @return boolean
+ * @return ClassLoader
*/
- public function load($className)
+ public function createLoader($baseDir='')
{
- if (false == isset($this->map[$className])) {
- return false;
- }
-
- return include_once $this->map[$className];
+ return new ClassMapLoader($this->map->getArrayCopy());
}
}
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Holds the ClassMapLoader class
+ *
+ * @author fqqdk <fqqdk@freemail.hu>
+ */
+
+/**
+ * Description of ClassMapLoader
+ */
+class ClassMapLoader implements ClassLoader
+{
+ /**
+ * @var array internal map of classes to files
+ */
+ private $map;
+
+ /**
+ * Constructor
+ *
+ * @param array $map the map of classes to files to use internally
+ * @param string $baseDir optional basedir to use with the files
+ *
+ * @return ClassMapLoader
+ */
+ public function __construct(array $map, $baseDir='')
+ {
+ $this->map = $map;
+ $this->baseDir = $baseDir;
+ }
+
+ /**
+ * Attempts to load a class
+ *
+ * @param string $className the name of the class to load
+ *
+ * @return boolean
+ */
+ public function load($className)
+ {
+ if (false == isset($this->map[$className])) {
+ return false;
+ }
+
+ return include_once $this->map[$className];
+ }
+}
+
+?>
@@ -8,20 +8,9 @@
/**
* The PhpCmcListener used by the API
*/
-class ApiListener implements PhpCmcListener
+class NullListener implements CollectListener, ClassListener
{
/**
- * This event is fired when an error occurs during class map collection
- *
- * @param string $error the error message
- *
- * @return void
- */
- public function error($error)
- {
- }
-
- /**
* This event is fired when the collector finds a class
*
* @param string $className the name of the found class
@@ -63,6 +52,17 @@ public function searchStarted()
public function searchCompleted()
{
}
+
+ /**
+ * This event is fired when an error occurs during class map collection
+ *
+ * @param string $error the error message
+ *
+ * @return void
+ */
+ public function error($error)
+ {
+ }
}
?>
@@ -37,7 +37,7 @@ class ParsingConvention implements PhpCmcNamingConvention
*
* @return ParsingConvention
*/
- public function __construct(PhpLinter $linter)
+ public function __construct(PhpLinter $linter)
{
$this->linter = $linter;
}
View
@@ -24,14 +24,14 @@ public static function registerLoaderFor($dir)
require_once dirname(__file__).'/PhpCmcApplication.php';
PhpCmcApplication::bootstrap();
- $listener = new ApiListener();
+ $listener = new NullListener();
$classMap = new ClassMap($listener);
$naming = new ParsingConvention(new PhpLinter($listener));
$collector = new ClassMapCollector($naming, $classMap);
$collector->collect(new RecursiveDirectoryWalker($dir), $listener);
- spl_autoload_register(array($classMap, 'load'));
+ spl_autoload_register(array($classMap->createLoader(), 'load'));
}
}
@@ -11,11 +11,6 @@
class PhpCmcApplication
{
/**
- * @var array classmap for autoloading
- */
- private static $classMap = array();
-
- /**
* @var OutputStream output stream
*/
private $output;
@@ -47,8 +42,12 @@ public static function bootstrap()
return false;
}
- self::$classMap = require 'phpcmc.classmap.php';
- spl_autoload_register(array(__class__, 'autoload'));
+ require_once dirname(__file__).'/ClassLoader.php';
+ require_once dirname(__file__).'/ClassMapLoader.php';
+
+ $loader = new ClassMapLoader(require 'phpcmc.classmap.php');
+
+ spl_autoload_register(array($loader, 'load'));
}
/**
@@ -75,22 +74,6 @@ public static function main(array $args)
}
/**
- * Autoloads a class
- *
- * @param string $className the class name
- *
- * @return boolean
- */
- public static function autoload($className)
- {
- if (false == isset(self::$classMap[$className])) {
- return false;
- }
-
- return include_once self::$classMap[$className];
- }
-
- /**
* Constructor
*
* @param OutputStream $output output stream
@@ -123,7 +123,7 @@ public function packageIsInstallable()
->runWith($this->runner);
$this->fail('This run should fail, but: '.$output);
} catch (ForeignError $ex) {
- $this->assertContains('Directory argument is mandatory', $ex->getError());
+ $this->assertContains('Directory argument is mandatory', $ex->getError(), $ex->getError());
$this->assertNotContains('Uncaught exception', $ex->getError());
}

0 comments on commit bd0523e

Please sign in to comment.