add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Core / AbstractFactory.php
1 <?php
2 /**
3 * The base abstract factory used by all CryptLib factories
4 *
5 * PHP version 5.3
6 *
7 * @category PHPCryptLib
8 * @package Core
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@@
13 */
14
15 namespace CryptLib\Core;
16
17 /**
18 * The base abstract factory used by all CryptLib factories
19 *
20 * @category PHPCryptLib
21 * @package Core
22 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
23 */
24 abstract class AbstractFactory {
25
26 /**
27 * Register a type with the factory by name
28 *
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
31 * by name.
32 *
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
38 *
39 * @return void
40 * @throws InvalidArgumentException If class does not implement interface
41 */
42 protected function registerType(
43 $type,
44 $implements,
45 $name,
46 $class,
47 $instantiate = false
48 ) {
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);
54 }
55 if ($instantiate) {
56 $class = new $class;
57 }
58
59 $this->{$type}[$name] = $class;
60 }
61
62 /**
63 * Load a set of classes from a directory into the factory
64 *
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
68 *
69 * @return void
70 */
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);
78 }
79 }
80 }
81
82 }