Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| FileSystemHandler | |
0.00% |
0 / 26 |
|
0.00% |
0 / 9 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getFileSystemId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPrefix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBaseDirectory | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getFileSystem | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| sanitizePath | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| __sleep | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __wakeup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllProperties | |
0.00% |
0 / 6 |
|
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) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\oatbox\filesystem; |
| 24 | |
| 25 | use oat\oatbox\log\LoggerAwareTrait; |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use ReflectionClass; |
| 28 | use ReflectionProperty; |
| 29 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 30 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 31 | |
| 32 | abstract class FileSystemHandler implements ServiceLocatorAwareInterface |
| 33 | { |
| 34 | use ServiceLocatorAwareTrait; |
| 35 | use LoggerAwareTrait; |
| 36 | |
| 37 | private const NOT_SERIALIZABLE_PROPERTIES = ['fileSystem', 'serviceLocator', 'logger']; |
| 38 | |
| 39 | /** |
| 40 | * @var mixed |
| 41 | */ |
| 42 | protected $fileSystemId; |
| 43 | |
| 44 | /** |
| 45 | * @var string |
| 46 | */ |
| 47 | protected $prefix; |
| 48 | |
| 49 | /** |
| 50 | * @var FileSystem |
| 51 | */ |
| 52 | protected $fileSystem; |
| 53 | |
| 54 | /** |
| 55 | * FileSystemHandler constructor. |
| 56 | * |
| 57 | * @param $id |
| 58 | * @param $prefix |
| 59 | */ |
| 60 | public function __construct($id, $prefix) |
| 61 | { |
| 62 | $this->fileSystemId = $id; |
| 63 | $this->prefix = $this->sanitizePath($prefix); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get id |
| 68 | * |
| 69 | * @return mixed |
| 70 | */ |
| 71 | public function getFileSystemId() |
| 72 | { |
| 73 | return $this->fileSystemId; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Return the prefix e.q. key of file system |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | public function getPrefix() |
| 82 | { |
| 83 | return $this->prefix; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get current base directory |
| 88 | * |
| 89 | * @return FileSystem |
| 90 | */ |
| 91 | protected function getBaseDirectory() |
| 92 | { |
| 93 | if (! $this->fileSystem) { |
| 94 | $this->fileSystem = $this->getServiceLocator() |
| 95 | ->get(FileSystemService::SERVICE_ID) |
| 96 | ->getDirectory($this->getFileSystemId()); |
| 97 | } |
| 98 | |
| 99 | return $this->fileSystem; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get current fileystem |
| 104 | * |
| 105 | * @return FileSystem |
| 106 | */ |
| 107 | public function getFileSystem() |
| 108 | { |
| 109 | if (! $this->fileSystem) { |
| 110 | $this->fileSystem = $this->getServiceLocator() |
| 111 | ->get(FileSystemService::SERVICE_ID) |
| 112 | ->getFileSystem($this->getFileSystemId()); |
| 113 | } |
| 114 | |
| 115 | return $this->fileSystem; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Sanitize path: |
| 120 | * - by replace \ to / for windows compatibility |
| 121 | * - trim . |
| 122 | * - trim / or \\ |
| 123 | * |
| 124 | * @param $path |
| 125 | * @return string |
| 126 | */ |
| 127 | protected function sanitizePath($path) |
| 128 | { |
| 129 | $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); |
| 130 | |
| 131 | $path = preg_replace('/' . preg_quote('./', '/') . '/', '', $path, 1); |
| 132 | $path = trim($path, '/'); |
| 133 | |
| 134 | return $path; |
| 135 | } |
| 136 | |
| 137 | public function __sleep() |
| 138 | { |
| 139 | return array_diff($this->getAllProperties(), self::NOT_SERIALIZABLE_PROPERTIES); |
| 140 | } |
| 141 | |
| 142 | public function __wakeup() |
| 143 | { |
| 144 | $this->setServiceLocator(ServiceManager::getServiceManager()); |
| 145 | } |
| 146 | |
| 147 | private function getAllProperties(): array |
| 148 | { |
| 149 | return array_map( |
| 150 | static function (ReflectionProperty $property): string { |
| 151 | return $property->getName(); |
| 152 | }, |
| 153 | (new ReflectionClass($this))->getProperties() |
| 154 | ); |
| 155 | } |
| 156 | } |