3 * The base abstract factory used by all CryptLib factories
7 * @category PHPCryptLib
9 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
10 * @copyright 2011 The Authors
11 * @license http://www.opensource.org/licenses/mit-license.html MIT License
12 * @version Build @@version@@
15 namespace CryptLib\Core
;
18 * The base abstract factory used by all CryptLib factories
20 * @category PHPCryptLib
22 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
24 abstract class AbstractFactory
{
27 * Register a type with the factory by name
29 * This is an internal method to check if a provided class name implements
30 * an interface, and if it does to append that class to an internal array
33 * @param string $type The name of the variable to store the class
34 * @param string $implements The interface to validate against
35 * @param string $name The name of this particular class
36 * @param string $class The fully qualified class name
37 * @param boolean $instantiate Should the class be stored instantiated
40 * @throws InvalidArgumentException If class does not implement interface
42 protected function registerType(
49 $name = strtolower($name);
50 $refl = new \
ReflectionClass($class);
51 if (!$refl->implementsInterface($implements)) {
52 $message = sprintf('Class must implement %s', $implements);
53 throw new \
InvalidArgumentException($message);
59 $this->{$type}[$name] = $class;
63 * Load a set of classes from a directory into the factory
65 * @param string $directory The directory to search for classes in
66 * @param string $namespace The namespace prefix for any found classes
67 * @param string $callback The callback with which to register the class
71 protected function loadFiles($directory, $namespace, $callback) {
72 foreach (new \
DirectoryIterator($directory) as $file) {
73 $filename = $file->getBasename();
74 if ($file->isFile() && substr($filename, -4) == '.php') {
75 $name = substr($filename, 0, -4);
76 $class = $namespace . $name;
77 call_user_func($callback, $name, $class);