Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LoadStylesheetClassesService | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
132 | |
0.00% |
0 / 1 |
| load | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| cssToArray | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |||
| getStylesheetRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoMediaManager\model\sharedStimulus\css\service; |
| 24 | |
| 25 | use oat\oatbox\filesystem\FilesystemException; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\taoMediaManager\model\sharedStimulus\css\dto\LoadStylesheet; |
| 28 | use oat\taoMediaManager\model\sharedStimulus\css\repository\StylesheetRepository; |
| 29 | |
| 30 | class LoadStylesheetClassesService extends ConfigurableService |
| 31 | { |
| 32 | public function load(LoadStylesheet $loadStylesheetDTO): array |
| 33 | { |
| 34 | $stylesheetRepository = $this->getStylesheetRepository(); |
| 35 | |
| 36 | $path = $stylesheetRepository->getPath($loadStylesheetDTO->getUri()); |
| 37 | |
| 38 | if ($path === '.') { |
| 39 | throw new \Exception('Shared stimulus stored as single file'); |
| 40 | } |
| 41 | |
| 42 | try { |
| 43 | $content = $stylesheetRepository->read( |
| 44 | $path . DIRECTORY_SEPARATOR . $loadStylesheetDTO->getStylesheetUri() |
| 45 | ); |
| 46 | |
| 47 | return $this->cssToArray($content); |
| 48 | } catch (FilesystemException $e) { |
| 49 | $this->logDebug( |
| 50 | sprintf( |
| 51 | 'Passage %s does not contain stylesheet %s. An empty array will be returned.', |
| 52 | $loadStylesheetDTO->getUri(), |
| 53 | $loadStylesheetDTO->getStylesheetUri() |
| 54 | ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | return []; |
| 59 | } |
| 60 | |
| 61 | private function cssToArray(string $css): array |
| 62 | { |
| 63 | $oldCssArr = explode("\n", $css); |
| 64 | array_shift($oldCssArr); |
| 65 | |
| 66 | $newCssArr = []; |
| 67 | foreach ($oldCssArr as $line) { |
| 68 | if (false === strpos($line, '{')) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | preg_match('~(?P<selector>[^{]+)(\{)(?P<rules>[^}]+)\}~', $line, $matches); |
| 73 | |
| 74 | foreach ($matches as $key => &$match) { |
| 75 | if (is_numeric($key)) { |
| 76 | continue; |
| 77 | } |
| 78 | $match = trim($match); |
| 79 | if ($key === 'rules') { |
| 80 | $ruleSet = array_filter(array_map('trim', explode(';', $match))); |
| 81 | $match = []; |
| 82 | foreach ($ruleSet as $rule) { |
| 83 | $rule = array_map('trim', explode(':', $rule)); |
| 84 | $match[$rule[0]] = $rule[1]; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | $newCssArr[$matches['selector']] = $matches['rules']; |
| 90 | } |
| 91 | return $newCssArr; |
| 92 | } |
| 93 | |
| 94 | private function getStylesheetRepository(): StylesheetRepository |
| 95 | { |
| 96 | return $this->getServiceLocator()->get(StylesheetRepository::class); |
| 97 | } |
| 98 | } |