Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ConfigurableService | |
0.00% |
0 / 29 |
|
0.00% |
0 / 6 |
342 | |
0.00% |
0 / 1 |
| setServiceManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSubService | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| setHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHeader | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getDefaultHeader | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| buildService | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2015-2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\oatbox\service; |
| 22 | |
| 23 | use oat\oatbox\Configurable; |
| 24 | use oat\oatbox\service\exception\InvalidService; |
| 25 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 26 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 27 | |
| 28 | /** |
| 29 | * Configurable base service |
| 30 | * |
| 31 | * inspired by Solarium\Core\Configurable by Bas de Nooijer |
| 32 | * https://github.com/basdenooijer/solarium/blob/master/library/Solarium/Core/Configurable.php |
| 33 | * |
| 34 | * @author Joel Bout <joel@taotesting.com> |
| 35 | * @deprecated New services must be registered using Dependency Injection Container |
| 36 | */ |
| 37 | abstract class ConfigurableService extends Configurable implements ServiceLocatorAwareInterface |
| 38 | { |
| 39 | use ServiceManagerAwareTrait; |
| 40 | |
| 41 | /** @var string Documentation header */ |
| 42 | protected $header = null; |
| 43 | |
| 44 | /** @var object[] */ |
| 45 | private $subServices = []; |
| 46 | |
| 47 | /** |
| 48 | * Get the service manager |
| 49 | * |
| 50 | * @deprecated Use $this->propagate instead |
| 51 | * |
| 52 | * @param $serviceManager |
| 53 | */ |
| 54 | public function setServiceManager($serviceManager) |
| 55 | { |
| 56 | $this->setServiceLocator($serviceManager); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get a subservice from the current service $options |
| 61 | * |
| 62 | * @param $id |
| 63 | * @param string $interface |
| 64 | * @return mixed |
| 65 | * @throws InvalidService |
| 66 | * @throws InvalidServiceManagerException |
| 67 | * @deprecated New services must be registered using Dependency Injection Container |
| 68 | */ |
| 69 | public function getSubService($id, $interface = null) |
| 70 | { |
| 71 | if (! isset($this->subServices[$id])) { |
| 72 | if ($this->hasOption($id)) { |
| 73 | $service = $this->buildService($this->getOption($id), $interface); |
| 74 | if ($service) { |
| 75 | $this->subServices[$id] = $service; |
| 76 | } else { |
| 77 | throw new ServiceNotFoundException($id); |
| 78 | } |
| 79 | } else { |
| 80 | throw new ServiceNotFoundException($id); |
| 81 | } |
| 82 | } |
| 83 | return $this->subServices[$id]; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Set the documentation header uses into config file |
| 88 | * |
| 89 | * @param $header |
| 90 | * @deprecated New services must be registered using Dependency Injection Container |
| 91 | */ |
| 92 | public function setHeader($header) |
| 93 | { |
| 94 | $this->header = $header; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Return the documentation header |
| 99 | * |
| 100 | * @return string |
| 101 | * @deprecated New services must be registered using Dependency Injection Container |
| 102 | */ |
| 103 | public function getHeader() |
| 104 | { |
| 105 | if (is_null($this->header)) { |
| 106 | return $this->getDefaultHeader(); |
| 107 | } else { |
| 108 | return $this->header; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the documentation header |
| 114 | * |
| 115 | * @return string |
| 116 | * @deprecated New services must be registered using Dependency Injection Container |
| 117 | */ |
| 118 | protected function getDefaultHeader() |
| 119 | { |
| 120 | return '<?php' . PHP_EOL |
| 121 | . '/**' . PHP_EOL |
| 122 | . ' * Default config header created during install' . PHP_EOL |
| 123 | . ' */' . PHP_EOL; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Build a sub service from current service $options |
| 128 | * |
| 129 | * @param $serviceDefinition |
| 130 | * @param string $interfaceName |
| 131 | * @return mixed |
| 132 | * @throws InvalidService |
| 133 | * @throws InvalidServiceManagerException |
| 134 | * @deprecated New services must be registered using Dependency Injection Container |
| 135 | */ |
| 136 | protected function buildService($serviceDefinition, $interfaceName = null) |
| 137 | { |
| 138 | if ($serviceDefinition instanceof ConfigurableService) { |
| 139 | if (is_null($interfaceName) || is_a($serviceDefinition, $interfaceName)) { |
| 140 | $this->propagate($serviceDefinition); |
| 141 | return $serviceDefinition; |
| 142 | } else { |
| 143 | throw new InvalidService('Service must implements ' . $interfaceName); |
| 144 | } |
| 145 | } elseif (is_array($serviceDefinition) && isset($serviceDefinition['class'])) { |
| 146 | $classname = $serviceDefinition['class']; |
| 147 | $options = isset($serviceDefinition['options']) ? $serviceDefinition['options'] : []; |
| 148 | if (is_null($interfaceName) || is_a($classname, $interfaceName, true)) { |
| 149 | return $this->getServiceManager()->build($classname, $options); |
| 150 | } else { |
| 151 | throw new InvalidService('Service must implements ' . $interfaceName); |
| 152 | } |
| 153 | } else { |
| 154 | throw new InvalidService(); |
| 155 | } |
| 156 | } |
| 157 | } |