Commit | Line | Data |
---|---|---|
14d4f286 S |
1 | <?php |
2 | /** | |
3 | * The core Encryption Factory | |
4 | * | |
5 | * PHP version 5.3 | |
6 | * | |
7 | * @category PHPCryptLib | |
8 | * @package Encryption | |
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\Encryption; | |
16 | ||
17 | use CryptLib\Cipher\Factory as CipherFactory; | |
18 | ||
19 | /** | |
20 | * The core Encryption Factory | |
21 | * | |
22 | * PHP version 5.3 | |
23 | * | |
24 | * @category PHPCryptLib | |
25 | * @package Encryption | |
26 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
27 | */ | |
28 | class Factory extends \CryptLib\Core\AbstractFactory { | |
29 | ||
30 | /** | |
31 | * @var Factory The Cipher Factory to use for this instance | |
32 | */ | |
33 | protected $cipherFactory = null; | |
34 | ||
35 | /** | |
36 | * @var array An array of PackingModes available for use | |
37 | */ | |
38 | protected $packingModes = array(); | |
39 | ||
40 | /** | |
41 | * Build the instance | |
42 | * | |
43 | * @param Factory $factory The Cipher Factory to use for this instance | |
44 | * | |
45 | * @return void | |
46 | */ | |
47 | public function __construct(CryptoLib\Cipher\Factory $factory = null) { | |
48 | if (is_null($factory)) { | |
49 | $factory = new CipherFactory(); | |
50 | } | |
51 | $this->cipherFactory = $factory; | |
52 | $this->loadPackingModes(); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Get a packing mode by name | |
57 | * | |
58 | * @param string|PackingMode $name The name of the packing mode or instance | |
59 | * | |
60 | * @return PackingMode The instantiated PackingMode class | |
61 | * @throws RuntimeException If the mode does not exist | |
62 | */ | |
63 | public function getPackingMode($name) { | |
64 | if (is_object($name) && $name instanceof PackingMode) { | |
65 | return $name; | |
66 | } | |
67 | $name = strtolower($name); | |
68 | if (isset($this->packingModes[$name])) { | |
69 | $class = $this->packingModes[$name]; | |
70 | return new $class; | |
71 | } | |
72 | $message = sprintf('Invalid Mode Supplied: %s', $name); | |
73 | throw new \RuntimeException($message); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Register a new packing mode by name | |
78 | * | |
79 | * @param string $name The name of the packing mode | |
80 | * @param string $class The class to instantiate for the mode | |
81 | * | |
82 | * @return Factory $this The current factory instance | |
83 | */ | |
84 | public function registerPackingMode($name, $class) { | |
85 | $this->registerType( | |
86 | 'packingModes', | |
87 | __NAMESPACE__ . '\\PackingMode', | |
88 | $name, | |
89 | $class | |
90 | ); | |
91 | return $this; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Load the core packing modes for this instance | |
96 | * | |
97 | * @return void | |
98 | */ | |
99 | protected function loadPackingModes() { | |
100 | $this->loadFiles( | |
101 | __DIR__ . '/PackingMode', | |
102 | __NAMESPACE__ . '\\PackingMode\\', | |
103 | array($this, 'registerPackingMode') | |
104 | ); | |
105 | } | |
106 | ||
107 | } |