Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Installer | |
0.00% |
0 / 45 |
|
0.00% |
0 / 6 |
272 | |
0.00% |
0 / 1 |
| install | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| setupServiceManager | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| installFilesystem | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| setupExtensionManager | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| validateOptions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
30 | |||
| getConfigPath | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 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\install; |
| 23 | |
| 24 | use League\Flysystem\InMemory\InMemoryFilesystemAdapter; |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\oatbox\service\exception\InvalidService; |
| 27 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 28 | use oat\oatbox\service\ServiceConfigDriver; |
| 29 | use oat\oatbox\service\ServiceManager; |
| 30 | use oat\oatbox\filesystem\FileSystemService; |
| 31 | use oat\oatbox\service\ServiceNotFoundException; |
| 32 | use common_report_Report as Report; |
| 33 | |
| 34 | /** |
| 35 | * A service to install oatbox functionality |
| 36 | * |
| 37 | * Sets up: |
| 38 | * configuration |
| 39 | * filesystems |
| 40 | */ |
| 41 | class Installer extends ConfigurableService |
| 42 | { |
| 43 | /** |
| 44 | * run the install |
| 45 | */ |
| 46 | public function install() |
| 47 | { |
| 48 | $this->validateOptions(); |
| 49 | |
| 50 | $this->setupServiceManager($this->getConfigPath()); |
| 51 | $this->setupExtensionManager(); |
| 52 | $this->installFilesystem(); |
| 53 | |
| 54 | return new Report(Report::TYPE_SUCCESS, 'Oatbox installed successfully'); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Setup the service manager with configuration driver associated to config path |
| 59 | * |
| 60 | * @param $configPath |
| 61 | * @return ServiceManager |
| 62 | * @throws \common_exception_Error |
| 63 | * @throws InvalidServiceManagerException |
| 64 | */ |
| 65 | public function setupServiceManager($configPath) |
| 66 | { |
| 67 | try { |
| 68 | $this->getServiceManager(); |
| 69 | } catch (InvalidServiceManagerException $e) { |
| 70 | if (! \helpers_File::emptyDirectory($configPath, true)) { |
| 71 | throw new \common_exception_Error('Unable to empty ' . $configPath . ' folder.'); |
| 72 | } |
| 73 | $driver = new ServiceConfigDriver(); |
| 74 | $configService = $driver->connect('config', [ |
| 75 | 'dir' => $configPath, |
| 76 | 'humanReadable' => true |
| 77 | ]); |
| 78 | $this->setServiceLocator(new ServiceManager($configService)); |
| 79 | } |
| 80 | |
| 81 | return $this->getServiceManager(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Install the filesystem service if not already installed |
| 86 | * |
| 87 | * @throws InvalidService If installed filesystem is not a FileSystemService |
| 88 | * @throws InvalidServiceManagerException |
| 89 | * @throws \common_Exception |
| 90 | */ |
| 91 | protected function installFilesystem() |
| 92 | { |
| 93 | try { |
| 94 | if (! ($this->getServiceManager()->get(FileSystemService::SERVICE_ID) instanceof FileSystemService)) { |
| 95 | throw new InvalidService('Your service must be a oat\oatbox\filesystem\FileSystemService'); |
| 96 | } |
| 97 | } catch (ServiceNotFoundException $e) { |
| 98 | $fileSystemService = new FileSystemService([ |
| 99 | FileSystemService::OPTION_FILE_PATH => $this->getOption('file_path'), |
| 100 | FileSystemService::OPTION_ADAPTERS => [ |
| 101 | 'default' => [ |
| 102 | 'class' => 'Local', |
| 103 | 'options' => [ |
| 104 | 'location' => $this->getOption('file_path') |
| 105 | ] |
| 106 | ], |
| 107 | 'memory' => [ |
| 108 | 'class' => InMemoryFilesystemAdapter::class |
| 109 | ] |
| 110 | ] |
| 111 | ]); |
| 112 | $this->getServiceManager()->register(FileSystemService::SERVICE_ID, $fileSystemService); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public function setupExtensionManager(): void |
| 117 | { |
| 118 | $this->getServiceManager()->register( |
| 119 | \common_ext_ExtensionsManager::SERVICE_ID, |
| 120 | new \common_ext_ExtensionsManager() |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Validate require option e.q. file_path & root_path |
| 126 | * |
| 127 | * @throws \common_exception_MissingParameter |
| 128 | */ |
| 129 | protected function validateOptions() |
| 130 | { |
| 131 | if (!$this->hasOption('root_path') || empty($this->getOption('root_path'))) { |
| 132 | throw new \common_exception_MissingParameter('root_path', __CLASS__); |
| 133 | } |
| 134 | if (!$this->hasOption('file_path') || empty($this->getOption('file_path'))) { |
| 135 | throw new \common_exception_MissingParameter('file_path', __CLASS__); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the path where to install config |
| 141 | * |
| 142 | * @return string |
| 143 | */ |
| 144 | protected function getConfigPath() |
| 145 | { |
| 146 | if ($this->hasOption('config_path') && ! empty($this->getOption('config_path'))) { |
| 147 | return $this->getOption('config_path'); |
| 148 | } |
| 149 | return rtrim($this->getOption('root_path'), '/\\') . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR; |
| 150 | } |
| 151 | } |