1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @copyright Metaways Infosystems GmbH, 2013 |
||
5 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
6 | * @copyright Aimeos (aimeos.org), 2015-2018 |
||
7 | * @package Controller |
||
8 | * @subpackage Jobs |
||
9 | */ |
||
10 | |||
11 | |||
12 | namespace Aimeos\Controller\Jobs; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Factory which can create all job controllers. |
||
17 | * |
||
18 | * @package Controller |
||
19 | * @subpackage Jobs |
||
20 | */ |
||
21 | class Factory |
||
22 | { |
||
23 | static private $prefix = '\\Aimeos\\Controller\\Jobs'; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Creates the required controller specified by the given path of controller names. |
||
28 | * |
||
29 | * Controllers are created by providing only the domain name, e.g. |
||
30 | * "stock" for the \Aimeos\Controller\Jobs\Stock\Standard. |
||
31 | * Please note, that only the default controllers can be created. If you need |
||
32 | * a specific implementation, you need to use the factory class of the |
||
33 | * controller to hand over specifc implementation names. |
||
34 | * |
||
35 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by controllers |
||
36 | * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap object |
||
37 | * @param string $path Name of the domain |
||
38 | * @throws \Aimeos\Controller\Jobs\Exception If the given path is invalid or the controllers wasn't found |
||
39 | */ |
||
40 | static public function createController( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Bootstrap $aimeos, $path ) |
||
41 | { |
||
42 | $path = strtolower( trim( $path, "/ \n\t\r\0\x0B" ) ); |
||
43 | |||
44 | if( empty( $path ) ) { |
||
45 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Controller path is empty' ) ); |
||
46 | } |
||
47 | |||
48 | $parts = explode( '/', $path ); |
||
49 | |||
50 | foreach( $parts as $key => $part ) |
||
51 | { |
||
52 | if( ctype_alnum( $part ) === false ) { |
||
53 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Invalid controller "%1$s" in "%2$s"', $part, $path ) ); |
||
54 | } |
||
55 | |||
56 | $parts[$key] = ucwords( $part ); |
||
57 | } |
||
58 | |||
59 | $factory = '\\Aimeos\\Controller\\Jobs\\' . join( '\\', $parts ) . '\\Factory'; |
||
60 | |||
61 | if( class_exists( $factory ) === false ) { |
||
62 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Class "%1$s" not found', $factory ) ); |
||
63 | } |
||
64 | |||
65 | $controller = call_user_func_array( array( $factory, 'createController' ), array( $context, $aimeos ) ); |
||
66 | |||
67 | if( $controller === false ) { |
||
68 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Invalid factory "%1$s"', $factory ) ); |
||
69 | } |
||
70 | |||
71 | return $controller; |
||
72 | } |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Returns all available controller instances. |
||
77 | * |
||
78 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by controllers |
||
79 | * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap object |
||
80 | * @param array $cntlPaths Associative list of the base path as key and all |
||
81 | * relative job controller paths (core and extensions) |
||
82 | * @return array Associative list of controller names as key and the class instance as value |
||
83 | */ |
||
84 | static public function getControllers( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Bootstrap $aimeos, array $cntlPaths ) |
||
85 | { |
||
86 | $cntlList = []; |
||
87 | $subFolder = trim( str_replace( '\\', '/', self::$prefix ), '/' ); |
||
88 | |||
89 | if( strncmp( $subFolder, 'Aimeos' . '/', 7 ) === 0 ) { |
||
90 | $subFolder = substr( $subFolder, 7 ); |
||
91 | } |
||
92 | |||
93 | foreach( $cntlPaths as $path => $list ) |
||
94 | { |
||
95 | foreach( $list as $relpath ) |
||
96 | { |
||
97 | $path .= DIRECTORY_SEPARATOR . str_replace( '/', DIRECTORY_SEPARATOR, $relpath . '/' . $subFolder ); |
||
98 | |||
99 | if( is_dir( $path ) ) |
||
100 | { |
||
101 | $it = new \DirectoryIterator( $path ); |
||
102 | $list = self::createControllers( $it, $context, $aimeos ); |
||
103 | |||
104 | $cntlList = array_merge( $cntlList, $list ); |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | ksort( $cntlList ); |
||
110 | |||
111 | return $cntlList; |
||
112 | } |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Instantiates all found factories and stores the controller instances in the class variable. |
||
117 | * |
||
118 | * @param \DirectoryIterator $dir Iterator over the (sub-)directory which might contain a factory |
||
119 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by controllers |
||
120 | * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap object |
||
121 | * @param string $prefix Part of the class name between "\Aimeos\Controller\Jobs" and "Factory" |
||
122 | * @throws \Aimeos\Controller\Jobs\Exception If factory name is invalid or if the controller couldn't be instantiated |
||
123 | */ |
||
124 | static protected function createControllers( \DirectoryIterator $dir, \Aimeos\MShop\Context\Item\Iface $context, |
||
125 | \Aimeos\Bootstrap $aimeos, $prefix = '' ) |
||
126 | { |
||
127 | $list = []; |
||
128 | |||
129 | foreach( $dir as $entry ) |
||
130 | { |
||
131 | if( $entry->getType() === 'dir' && $entry->isDot() === false ) |
||
132 | { |
||
133 | $name = strtolower( $entry->getBaseName() ); |
||
134 | $it = new \DirectoryIterator( $entry->getPathName() ); |
||
135 | $pref = ( $prefix !== '' ? $prefix . '/' : '' ) . $name; |
||
136 | $subList = self::createControllers( $it, $context, $aimeos, $pref ); |
||
137 | |||
138 | $list = array_merge( $list, $subList ); |
||
139 | } |
||
140 | else if( $prefix !== '' && $entry->getType() === 'file' |
||
141 | && ( $name = $entry->getBaseName( '.php' ) ) === 'Factory' ) |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
142 | { |
||
143 | $list[$prefix] = self::createController( $context, $aimeos, $prefix ); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | return $list; |
||
148 | } |
||
149 | } |
||
150 |