| @@ 9-78 (lines=70) @@ | ||
| 6 | use Error; |
|
| 7 | use Exception; |
|
| 8 | ||
| 9 | abstract class FactoryPlugin implements FactoryPluginInterface |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @var bool |
|
| 13 | */ |
|
| 14 | protected $registered = false; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @override |
|
| 18 | * @inheritDoc |
|
| 19 | */ |
|
| 20 | public function registerPlugin(FactoryInterface $factory) |
|
| 21 | { |
|
| 22 | try |
|
| 23 | { |
|
| 24 | $this->register($factory); |
|
| 25 | $this->registered = true; |
|
| 26 | } |
|
| 27 | catch (Error $ex) |
|
| 28 | { |
|
| 29 | $this->throwException($ex); |
|
| 30 | } |
|
| 31 | catch (Exception $ex) |
|
| 32 | { |
|
| 33 | $this->throwException($ex); |
|
| 34 | } |
|
| 35 | ||
| 36 | return $this; |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * @override |
|
| 41 | * @inheritDoc |
|
| 42 | */ |
|
| 43 | public function unregisterPlugin(FactoryInterface $factory) |
|
| 44 | { |
|
| 45 | if ($this->registered) |
|
| 46 | { |
|
| 47 | $this->unregister($factory); |
|
| 48 | $this->registered = false; |
|
| 49 | } |
|
| 50 | ||
| 51 | return $this; |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * Define how plugin should be registered. |
|
| 56 | * |
|
| 57 | * @param FactoryInterface $factory |
|
| 58 | */ |
|
| 59 | protected function register(FactoryInterface $factory) |
|
| 60 | {} |
|
| 61 | ||
| 62 | /** |
|
| 63 | * Define how plugin should be unregistered. |
|
| 64 | * |
|
| 65 | * @param FactoryInterface $factory |
|
| 66 | */ |
|
| 67 | protected function unregister(FactoryInterface $factory) |
|
| 68 | {} |
|
| 69 | ||
| 70 | /** |
|
| 71 | * @param Error|Exception $ex |
|
| 72 | * @throws ExecutionException |
|
| 73 | */ |
|
| 74 | private function throwException($ex) |
|
| 75 | { |
|
| 76 | throw new ExecutionException("FactoryPlugin [" . get_class($this) . "] raised an error.", 0, $ex); |
|
| 77 | } |
|
| 78 | } |
|
| 79 | ||
| @@ 9-78 (lines=70) @@ | ||
| 6 | use Error; |
|
| 7 | use Exception; |
|
| 8 | ||
| 9 | abstract class SimpleFactoryPlugin implements SimpleFactoryPluginInterface |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @var bool |
|
| 13 | */ |
|
| 14 | private $registered; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @override |
|
| 18 | * @inheritDoc |
|
| 19 | */ |
|
| 20 | public function registerPlugin(SimpleFactoryInterface $factory) |
|
| 21 | { |
|
| 22 | try |
|
| 23 | { |
|
| 24 | $this->register($factory); |
|
| 25 | $this->registered = true; |
|
| 26 | } |
|
| 27 | catch (Error $ex) |
|
| 28 | { |
|
| 29 | $this->throwException($ex); |
|
| 30 | } |
|
| 31 | catch (Exception $ex) |
|
| 32 | { |
|
| 33 | $this->throwException($ex); |
|
| 34 | } |
|
| 35 | ||
| 36 | return $this; |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * @override |
|
| 41 | * @inheritDoc |
|
| 42 | */ |
|
| 43 | public function unregisterPlugin(SimpleFactoryInterface $factory) |
|
| 44 | { |
|
| 45 | if ($this->registered) |
|
| 46 | { |
|
| 47 | $this->unregister($factory); |
|
| 48 | $this->registered = false; |
|
| 49 | } |
|
| 50 | ||
| 51 | return $this; |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * Define how plugin should be registered. |
|
| 56 | * |
|
| 57 | * @param SimpleFactoryInterface $factory |
|
| 58 | */ |
|
| 59 | protected function register(SimpleFactoryInterface $factory) |
|
| 60 | {} |
|
| 61 | ||
| 62 | /** |
|
| 63 | * Define how plugin should be unregistered. |
|
| 64 | * |
|
| 65 | * @param SimpleFactoryInterface $factory |
|
| 66 | */ |
|
| 67 | protected function unregister(SimpleFactoryInterface $factory) |
|
| 68 | {} |
|
| 69 | ||
| 70 | /** |
|
| 71 | * @param Error|Exception $ex |
|
| 72 | * @throws ExecutionException |
|
| 73 | */ |
|
| 74 | private function throwException($ex) |
|
| 75 | { |
|
| 76 | throw new ExecutionException("SimpleFactoryPlugin [" . get_class($this) . "] raised an error.", 0, $ex); |
|
| 77 | } |
|
| 78 | } |
|
| 79 | ||