Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
11.86% |
14 / 118 |
|
8.70% |
2 / 23 |
CRAP | |
0.00% |
0 / 1 |
| common_ext_Extension | |
11.86% |
14 / 118 |
|
8.70% |
2 / 23 |
2361.09 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConstants | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setConfig | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getConfig | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| unsetConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAuthor | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDir | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| hasConstant | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getConstant | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getAllModules | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
210 | |||
| getModule | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getDependencies | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getManifest | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
5.02 | |||
| getPhpNamespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isLoaded | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| load | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| getExtensionManager | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getConfigHeader | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getUpdater | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
3.14 | |||
| 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) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * 2013-2014 (update and modification) Open Assessment Technologies SA; |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | use oat\oatbox\extension\exception\ManifestException; |
| 27 | use common_ext_UpdaterNotFoundException as UpdaterNotFoundException; |
| 28 | use oat\oatbox\service\ServiceManagerAwareInterface; |
| 29 | use oat\oatbox\service\ServiceManagerAwareTrait; |
| 30 | use oat\oatbox\service\ServiceNotFoundException; |
| 31 | use oat\oatbox\service\ConfigurableService; |
| 32 | use oat\oatbox\config\ConfigurationService; |
| 33 | use oat\oatbox\extension\exception\ManifestNotFoundException; |
| 34 | use oat\oatbox\extension\Manifest; |
| 35 | |
| 36 | /** |
| 37 | * Short description of class common_ext_Extension |
| 38 | * |
| 39 | * @access public |
| 40 | * @author lionel.lecaque@tudor.lu |
| 41 | * @package generis |
| 42 | * @see @license GNU General Public (GPL) Version 2 http://www.opensource.org/licenses/gpl-2.0.php |
| 43 | |
| 44 | */ |
| 45 | class common_ext_Extension implements ServiceManagerAwareInterface |
| 46 | { |
| 47 | use ServiceManagerAwareTrait; |
| 48 | |
| 49 | /** |
| 50 | * Filename of the manifest |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | public const MANIFEST_NAME = 'manifest.php'; |
| 55 | |
| 56 | /** |
| 57 | * List of extension dependencies |
| 58 | * |
| 59 | * @var array |
| 60 | */ |
| 61 | private $dependencies = []; |
| 62 | |
| 63 | /** |
| 64 | * Short description of attribute id |
| 65 | * |
| 66 | * @access private |
| 67 | * @var string |
| 68 | */ |
| 69 | private $id = ''; |
| 70 | |
| 71 | /** |
| 72 | * The manifest of the extension |
| 73 | * |
| 74 | * @var Manifest |
| 75 | */ |
| 76 | protected $manifest; |
| 77 | |
| 78 | /** |
| 79 | * Whenever or not an extension has already been loaded |
| 80 | * |
| 81 | * @access private |
| 82 | * @var boolean |
| 83 | */ |
| 84 | protected $loaded = false; |
| 85 | |
| 86 | /** |
| 87 | * Should not be called directly, please use ExtensionsManager |
| 88 | * |
| 89 | * @access public |
| 90 | * @author Joel Bout, <joel@taotesting.com> |
| 91 | * |
| 92 | * @param string $id |
| 93 | */ |
| 94 | public function __construct($id) |
| 95 | { |
| 96 | $this->id = $id; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * returns the id of the extension |
| 101 | * |
| 102 | * @access public |
| 103 | * @author firstname and lastname of author, <author@example.org> |
| 104 | * @return string |
| 105 | */ |
| 106 | public function getId() |
| 107 | { |
| 108 | return $this->id; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * returns all constants of the extension |
| 113 | * |
| 114 | * @access public |
| 115 | * @author firstname and lastname of author, <author@example.org> |
| 116 | * @return array |
| 117 | */ |
| 118 | public function getConstants() |
| 119 | { |
| 120 | return (array) $this->getManifest()->getConstants(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * checks if a configuration value exists |
| 125 | * |
| 126 | * @param string $key |
| 127 | * @return boolean |
| 128 | */ |
| 129 | public function hasConfig($key) |
| 130 | { |
| 131 | return $this->getServiceLocator()->has($this->getId() . '/' . $key); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * sets a configuration value |
| 136 | * |
| 137 | * @param string $key |
| 138 | * @param string $value |
| 139 | * @return bool always returns true for backward compatibility |
| 140 | * @throws common_exception_Error On error |
| 141 | */ |
| 142 | public function setConfig($key, $value) |
| 143 | { |
| 144 | if (! is_object($value) || ! $value instanceof ConfigurableService) { |
| 145 | $value = new ConfigurationService([ConfigurationService::OPTION_CONFIG => $value]); |
| 146 | } |
| 147 | $value->setHeader($this->getConfigHeader($key)); |
| 148 | $this->registerService($this->getId() . '/' . $key, $value); |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * retrieves a configuration value |
| 154 | * returns false if not found |
| 155 | * |
| 156 | * @param string $key |
| 157 | * @return mixed |
| 158 | */ |
| 159 | public function getConfig($key) |
| 160 | { |
| 161 | if (! $this->getServiceLocator()->has($this->getId() . '/' . $key)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | try { |
| 166 | $config = $this->getServiceLocator()->get($this->getId() . '/' . $key); |
| 167 | if ($config instanceof ConfigurationService) { |
| 168 | $config = $config->getConfig(); |
| 169 | } |
| 170 | } catch (ServiceNotFoundException $e) { |
| 171 | $config = false; |
| 172 | } |
| 173 | return $config; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * removes a configuration entry |
| 178 | * |
| 179 | * @param string $key |
| 180 | * @return mixed |
| 181 | */ |
| 182 | public function unsetConfig($key) |
| 183 | { |
| 184 | return $this->getServiceManager()->unregister($this->getId() . '/' . $key); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * returns the version of the extension |
| 189 | * |
| 190 | * @access public |
| 191 | * @author firstname and lastname of author, <author@example.org> |
| 192 | * @return string |
| 193 | */ |
| 194 | public function getVersion() |
| 195 | { |
| 196 | return (string) $this->getManifest()->getVersion(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * returns the author of the extension |
| 201 | * |
| 202 | * @access public |
| 203 | * @author firstname and lastname of author, <author@example.org> |
| 204 | * @return string |
| 205 | */ |
| 206 | public function getAuthor() |
| 207 | { |
| 208 | return (string) $this->getManifest()->getAuthor(); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * returns the name of the extension |
| 213 | * |
| 214 | * @access public |
| 215 | * @author firstname and lastname of author, <author@example.org> |
| 216 | * @return string |
| 217 | */ |
| 218 | public function getName() |
| 219 | { |
| 220 | return (string) $this->getManifest()->getName(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * returns the base dir of the extension |
| 225 | * |
| 226 | * @access public |
| 227 | * @author firstname and lastname of author, <author@example.org> |
| 228 | * @return string |
| 229 | * @throws common_ext_ExtensionException |
| 230 | */ |
| 231 | public function getDir() |
| 232 | { |
| 233 | if (! defined('EXTENSION_PATH')) { |
| 234 | throw new common_ext_ExtensionException('System constants are not yet defined'); |
| 235 | } |
| 236 | |
| 237 | return EXTENSION_PATH . $this->getId() . DIRECTORY_SEPARATOR; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @param $key |
| 242 | * |
| 243 | * @return bool |
| 244 | */ |
| 245 | public function hasConstant($key) |
| 246 | { |
| 247 | $constants = $this->getConstants(); |
| 248 | return isset($constants[$key]); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Retrieves a constant from the manifest.php file of the extension. |
| 253 | * |
| 254 | * @author Joel Bout, <joel@taotesting.com> |
| 255 | * @param string $key |
| 256 | * @return mixed |
| 257 | * @throws common_exception_Error If the constant cannot be found. |
| 258 | */ |
| 259 | public function getConstant($key) |
| 260 | { |
| 261 | $returnValue = null; |
| 262 | |
| 263 | $constants = $this->getConstants(); |
| 264 | if (isset($constants[$key])) { |
| 265 | $returnValue = $constants[$key]; |
| 266 | } elseif (defined($key)) { |
| 267 | common_logger::w('constant outside of extension called: ' . $key); |
| 268 | $returnValue = constant($key); |
| 269 | } else { |
| 270 | throw new common_exception_Error('Unknown constant \'' . $key . '\' for extension ' . $this->id); |
| 271 | } |
| 272 | |
| 273 | return $returnValue; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * get all modules of the extension |
| 278 | * by searching the actions directory, not the ontology |
| 279 | * |
| 280 | * @access public |
| 281 | * @author firstname and lastname of author, <author@example.org> |
| 282 | * @return array |
| 283 | */ |
| 284 | public function getAllModules() |
| 285 | { |
| 286 | $returnValue = []; |
| 287 | |
| 288 | // routes |
| 289 | $namespaces = []; |
| 290 | foreach ($this->getManifest()->getRoutes() as $mapedPath => $ns) { |
| 291 | $namespaces[] = trim($ns, '\\'); |
| 292 | } |
| 293 | if (!empty($namespaces)) { |
| 294 | common_Logger::d('Namespace not empty for extension ' . $this->getId()); |
| 295 | $recDir = new RecursiveDirectoryIterator($this->getDir()); |
| 296 | $recIt = new RecursiveIteratorIterator($recDir); |
| 297 | $regexIt = new RegexIterator($recIt, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH); |
| 298 | foreach ($regexIt as $entry) { |
| 299 | $info = helpers_PhpTools::getClassInfo($entry[0]); |
| 300 | if (!empty($info['ns'])) { |
| 301 | $ns = trim($info['ns'], '\\'); |
| 302 | if (!empty($info['ns']) && in_array($ns, $namespaces)) { |
| 303 | $returnValue[$info['class']] = $ns . '\\' . $info['class']; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | // legacy |
| 309 | if ($this->hasConstant('DIR_ACTIONS') && file_exists($this->getConstant('DIR_ACTIONS'))) { |
| 310 | $dir = new DirectoryIterator($this->getConstant('DIR_ACTIONS')); |
| 311 | foreach ($dir as $fileinfo) { |
| 312 | if (preg_match('/^class\.[^.]*\.php$/', $fileinfo->getFilename())) { |
| 313 | $module = substr($fileinfo->getFilename(), 6, -4); |
| 314 | $returnValue[$module] = $this->getId() . '_actions_' . $module; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // validate the classes |
| 320 | foreach (array_keys($returnValue) as $key) { |
| 321 | $class = $returnValue[$key]; |
| 322 | if (!class_exists($class)) { |
| 323 | common_Logger::w($class . ' not found'); |
| 324 | unset($returnValue[$key]); |
| 325 | } elseif (!is_subclass_of($class, 'Module')) { |
| 326 | common_Logger::w($class . ' does not inherit Module'); |
| 327 | unset($returnValue[$key]); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return (array) $returnValue; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * returns a module by ID |
| 336 | * |
| 337 | * @access public |
| 338 | * @author firstname and lastname of author, <author@example.org> |
| 339 | * @param string $id |
| 340 | * @return Module |
| 341 | */ |
| 342 | public function getModule($id) |
| 343 | { |
| 344 | $returnValue = null; |
| 345 | |
| 346 | $className = $this->getId() . '_actions_' . $id; |
| 347 | if (class_exists($className)) { |
| 348 | $returnValue = new $className(); |
| 349 | } else { |
| 350 | common_Logger::e('could not load ' . $className); |
| 351 | } |
| 352 | |
| 353 | return $returnValue; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Returns the extension the current extension depends on recursively |
| 358 | * |
| 359 | * @access public |
| 360 | * @return array Where key is name of extension and value is required version. |
| 361 | */ |
| 362 | public function getDependencies() |
| 363 | { |
| 364 | if (empty($this->dependencies)) { |
| 365 | foreach ($this->getManifest()->getDependencies() as $id => $version) { |
| 366 | $this->dependencies[$id] = $version; |
| 367 | $dependence = $this->getExtensionManager()->getExtensionById($id); |
| 368 | $this->dependencies = array_merge($this->dependencies, $dependence->getDependencies()); |
| 369 | } |
| 370 | } |
| 371 | return $this->dependencies; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Returns the manifest of the extension |
| 376 | * |
| 377 | * @return Manifest |
| 378 | * @throws ManifestNotFoundException |
| 379 | */ |
| 380 | public function getManifest() |
| 381 | { |
| 382 | if (! $this->manifest) { |
| 383 | $manifestFile = $this->getDir() . self::MANIFEST_NAME; |
| 384 | if (is_file($manifestFile) && is_readable($manifestFile)) { |
| 385 | $this->manifest = new Manifest($manifestFile); |
| 386 | } else { |
| 387 | throw new ManifestNotFoundException( |
| 388 | "Extension Manifest not found for extension '" . $this->id . "'.", |
| 389 | $this->id |
| 390 | ); |
| 391 | } |
| 392 | } |
| 393 | $this->manifest->setServiceLocator($this->getServiceLocator()); |
| 394 | return $this->manifest; |
| 395 | } |
| 396 | |
| 397 | public function getPhpNamespace() |
| 398 | { |
| 399 | return $this->getManifest()->getPhpNamespace(); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Whenever or not the extension and it's constants have been loaded |
| 404 | * @return boolean |
| 405 | */ |
| 406 | public function isLoaded() |
| 407 | { |
| 408 | return $this->loaded; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Loads the extension if it hasn't been loaded (using load), yet |
| 413 | * All the dependent extensions will be loaded too. |
| 414 | */ |
| 415 | public function load() |
| 416 | { |
| 417 | if (!$this->isLoaded()) { |
| 418 | try { |
| 419 | $dependencies = $this->getManifest()->getDependencies(); |
| 420 | foreach ($dependencies as $extId => $extVersion) { |
| 421 | $this->getExtensionManager()->getExtensionById($extId); |
| 422 | } |
| 423 | } catch (ManifestNotFoundException $e) { |
| 424 | throw new common_ext_MissingExtensionException( |
| 425 | $e->getExtensionId() . ' not found but required for ' . $this->getId(), |
| 426 | $e->getExtensionId() |
| 427 | ); |
| 428 | } |
| 429 | |
| 430 | $loader = new common_ext_ExtensionLoader($this); |
| 431 | $loader->load(); |
| 432 | //load all dependent extensions |
| 433 | $this->loaded = true; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Get the ExtensionManager service |
| 439 | * |
| 440 | * @return common_ext_ExtensionsManager|mixed |
| 441 | */ |
| 442 | protected function getExtensionManager() |
| 443 | { |
| 444 | if ($this->getServiceLocator()->has(common_ext_ExtensionsManager::SERVICE_ID)) { |
| 445 | $service = $this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID); |
| 446 | } else { |
| 447 | $service = new common_ext_ExtensionsManager(); |
| 448 | $this->getServiceLocator()->propagate($service); |
| 449 | } |
| 450 | return $service; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Get the documentation header for extension config located at key path |
| 455 | * |
| 456 | * @param $key |
| 457 | * @return null|string |
| 458 | */ |
| 459 | public function getConfigHeader($key) |
| 460 | { |
| 461 | $path = $this->getDir() . 'config/header/' . $key . '.conf.php'; |
| 462 | if (is_readable($path) && is_file($path)) { |
| 463 | return file_get_contents($path); |
| 464 | } |
| 465 | return null; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * @throws ManifestException |
| 470 | * @throws ManifestNotFoundException |
| 471 | * @throws common_ext_UpdaterNotFoundException |
| 472 | */ |
| 473 | public function getUpdater(): common_ext_ExtensionUpdater |
| 474 | { |
| 475 | $updaterClass = $this->getManifest()->getUpdateHandler(); |
| 476 | if ($updaterClass === null) { |
| 477 | throw new UpdaterNotFoundException('No Updater found for ' . $this->getName()); |
| 478 | } elseif (!class_exists($updaterClass)) { |
| 479 | throw new ManifestException('Updater ' . $updaterClass . ' not found'); |
| 480 | } |
| 481 | /** @var common_ext_ExtensionUpdater $updater */ |
| 482 | $updater = new $updaterClass($this); |
| 483 | $updater->setServiceLocator($this->getServiceLocator()); |
| 484 | return $updater; |
| 485 | } |
| 486 | } |