Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ActionService | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
| resolve | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getAvailableActions | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| getCache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getActionsInDirectory | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| 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 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\action; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\oatbox\cache\SimpleCache; |
| 26 | use common_ext_ExtensionsManager; |
| 27 | |
| 28 | class ActionService extends ConfigurableService |
| 29 | { |
| 30 | public const SERVICE_ID = 'generis/actionService'; |
| 31 | |
| 32 | public static $blackList = [ |
| 33 | '\\oatbox\\composer\\ExtensionInstaller', |
| 34 | '\\oatbox\\composer\\ExtensionInstallerPlugin' |
| 35 | ]; |
| 36 | |
| 37 | /** |
| 38 | * |
| 39 | * @param string $actionIdentifier |
| 40 | * @return Action |
| 41 | */ |
| 42 | public function resolve($actionIdentifier) |
| 43 | { |
| 44 | $action = null; |
| 45 | if ($this->getServiceLocator()->has($actionIdentifier)) { |
| 46 | $action = $this->getServiceManager()->get($actionIdentifier); |
| 47 | } elseif (class_exists($actionIdentifier) && is_subclass_of($actionIdentifier, Action::class)) { |
| 48 | $action = new $actionIdentifier(); |
| 49 | $this->propagate($action); |
| 50 | } else { |
| 51 | throw new ResolutionException('Unknown action ' . $actionIdentifier); |
| 52 | } |
| 53 | return $action; |
| 54 | } |
| 55 | |
| 56 | public function getAvailableActions() |
| 57 | { |
| 58 | $actions = $this->getCache()->get(__FUNCTION__); |
| 59 | if (is_null($actions)) { |
| 60 | $actions = []; |
| 61 | $installedExtensions = $this |
| 62 | ->getServiceLocator() |
| 63 | ->get(common_ext_ExtensionsManager::SERVICE_ID) |
| 64 | ->getInstalledExtensions(); |
| 65 | |
| 66 | foreach ($installedExtensions as $ext) { |
| 67 | $actions = array_merge($actions, $this->getActionsInDirectory($ext->getDir())); |
| 68 | } |
| 69 | $actions = array_merge($actions, $this->getActionsInDirectory(VENDOR_PATH . 'oat-sa')); |
| 70 | $this->getCache()->set(__FUNCTION__, $actions); |
| 71 | } |
| 72 | return $actions; |
| 73 | } |
| 74 | |
| 75 | protected function getCache(): SimpleCache |
| 76 | { |
| 77 | return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID); |
| 78 | } |
| 79 | |
| 80 | protected function getActionsInDirectory($dir) |
| 81 | { |
| 82 | $classNames = []; |
| 83 | $recIt = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)); |
| 84 | $regexIt = new \RegexIterator($recIt, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH); |
| 85 | foreach ($regexIt as $entry) { |
| 86 | $info = \helpers_PhpTools::getClassInfo($entry[0]); |
| 87 | $fullname = empty($info['ns']) |
| 88 | ? $info['class'] |
| 89 | : $info['ns'] . '\\' . $info['class']; |
| 90 | if (!in_array($fullname, self::$blackList) && is_subclass_of($fullname, Action::class)) { |
| 91 | $classNames[] = $fullname; |
| 92 | } |
| 93 | } |
| 94 | return $classNames; |
| 95 | } |
| 96 | } |