o2system /
framework
| 1 | <?php |
||||||||
| 2 | /** |
||||||||
| 3 | * This file is part of the O2System Framework package. |
||||||||
| 4 | * |
||||||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||||||
| 6 | * file that was distributed with this source code. |
||||||||
| 7 | * |
||||||||
| 8 | * @author Steeve Andrian Salim |
||||||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||||||
| 10 | */ |
||||||||
| 11 | |||||||||
| 12 | // ------------------------------------------------------------------------ |
||||||||
| 13 | |||||||||
| 14 | namespace O2System\Framework\Services; |
||||||||
| 15 | |||||||||
| 16 | // ------------------------------------------------------------------------ |
||||||||
| 17 | |||||||||
| 18 | use O2System\Cache\Item; |
||||||||
| 19 | use O2System\Framework\DataStructures; |
||||||||
| 20 | use O2System\Kernel\Cli\Writers\Format; |
||||||||
| 21 | use Psr\Cache\CacheItemPoolInterface; |
||||||||
| 22 | |||||||||
| 23 | /** |
||||||||
| 24 | * Class Language |
||||||||
| 25 | * |
||||||||
| 26 | * @package O2System\Framework\Services |
||||||||
| 27 | */ |
||||||||
| 28 | class Language extends \O2System\Kernel\Services\Language |
||||||||
| 29 | { |
||||||||
| 30 | /** |
||||||||
| 31 | * Language::$registry |
||||||||
| 32 | * |
||||||||
| 33 | * Language registries. |
||||||||
| 34 | * |
||||||||
| 35 | * @var array |
||||||||
| 36 | */ |
||||||||
| 37 | private $registry = []; |
||||||||
| 38 | |||||||||
| 39 | // ------------------------------------------------------------------------ |
||||||||
| 40 | |||||||||
| 41 | /** |
||||||||
| 42 | * Language::__construct |
||||||||
| 43 | */ |
||||||||
| 44 | public function __construct() |
||||||||
| 45 | { |
||||||||
| 46 | parent::__construct(); |
||||||||
| 47 | |||||||||
| 48 | $this->addFilePaths([PATH_FRAMEWORK, PATH_APP]); |
||||||||
| 49 | } |
||||||||
| 50 | |||||||||
| 51 | // ------------------------------------------------------------------------ |
||||||||
| 52 | |||||||||
| 53 | /** |
||||||||
| 54 | * Language::loadRegistry |
||||||||
| 55 | * |
||||||||
| 56 | * Load language registry. |
||||||||
| 57 | * |
||||||||
| 58 | * @return void |
||||||||
| 59 | * @throws \Psr\Cache\InvalidArgumentException |
||||||||
| 60 | */ |
||||||||
| 61 | public function loadRegistry() |
||||||||
| 62 | { |
||||||||
| 63 | if (empty($this->registry)) { |
||||||||
| 64 | $cacheItemPool = cache()->getItemPool('default'); |
||||||||
| 65 | |||||||||
| 66 | if (cache()->hasItemPool('registry')) { |
||||||||
| 67 | $cacheItemPool = cache()->getItemPool('registry'); |
||||||||
| 68 | } |
||||||||
| 69 | |||||||||
| 70 | if ($cacheItemPool instanceof CacheItemPoolInterface) { |
||||||||
| 71 | if ($cacheItemPool->hasItem('o2languages')) { |
||||||||
| 72 | $this->registry = $cacheItemPool->getItem('o2languages')->get(); |
||||||||
| 73 | } else { |
||||||||
| 74 | $this->registry = $this->fetchRegistry(); |
||||||||
| 75 | $cacheItemPool->save(new Item('o2languages', $this->registry, false)); |
||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||||
| 76 | } |
||||||||
| 77 | } else { |
||||||||
| 78 | $this->registry = $this->fetchRegistry(); |
||||||||
| 79 | } |
||||||||
| 80 | } |
||||||||
| 81 | } |
||||||||
| 82 | |||||||||
| 83 | // ------------------------------------------------------------------------ |
||||||||
| 84 | |||||||||
| 85 | /** |
||||||||
| 86 | * Language::fetchRegistry |
||||||||
| 87 | * |
||||||||
| 88 | * Fetch language registry. |
||||||||
| 89 | * |
||||||||
| 90 | * @return array |
||||||||
| 91 | */ |
||||||||
| 92 | public function fetchRegistry() |
||||||||
| 93 | { |
||||||||
| 94 | $registry = []; |
||||||||
| 95 | |||||||||
| 96 | $directory = new \RecursiveIteratorIterator(new \RecursiveCallbackFilterIterator( |
||||||||
| 97 | new \RecursiveDirectoryIterator(PATH_ROOT, |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 98 | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS), |
||||||||
| 99 | function ($current, $key, $iterator) { |
||||||||
|
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $callback of RecursiveCallbackFilterIterator::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The parameter
$iterator is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 100 | if ($current->isDir()) { |
||||||||
| 101 | // exclude build directory |
||||||||
| 102 | if (in_array($current->getFilename(), [ |
||||||||
| 103 | 'node_modules' |
||||||||
| 104 | ])) { |
||||||||
| 105 | return false; |
||||||||
| 106 | } |
||||||||
| 107 | } |
||||||||
| 108 | |||||||||
| 109 | return true; |
||||||||
| 110 | })); |
||||||||
| 111 | |||||||||
| 112 | $packagesIterator = new \RegexIterator($directory, '/^.+\.json$/i', \RecursiveRegexIterator::GET_MATCH); |
||||||||
| 113 | |||||||||
| 114 | foreach ($packagesIterator as $packageJsonFiles) { |
||||||||
| 115 | foreach ($packageJsonFiles as $packageJsonFile) { |
||||||||
| 116 | $packageJsonFile = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $packageJsonFile); |
||||||||
| 117 | $packageJsonFileInfo = pathinfo($packageJsonFile); |
||||||||
| 118 | |||||||||
| 119 | $files[] = $packageJsonFileInfo; |
||||||||
| 120 | |||||||||
| 121 | if ($packageJsonFileInfo[ 'filename' ] === 'language') { |
||||||||
| 122 | if (is_cli()) { |
||||||||
| 123 | output()->verbose( |
||||||||
|
0 ignored issues
–
show
The method
verbose() does not exist on O2System\Kernel\Http\Output.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||||
| 124 | (new Format()) |
||||||||
| 125 | ->setString(language()->getLine('CLI_REGISTRY_LANGUAGE_VERB_FETCH_MANIFEST_START', |
||||||||
| 126 | [str_replace(PATH_ROOT, '/', $packageJsonFile)])) |
||||||||
| 127 | ->setNewLinesAfter(1) |
||||||||
| 128 | ); |
||||||||
| 129 | } |
||||||||
| 130 | |||||||||
| 131 | $package = new DataStructures\Language(dirname($packageJsonFile)); |
||||||||
| 132 | |||||||||
| 133 | if ($package->isValid()) { |
||||||||
| 134 | if (is_cli()) { |
||||||||
| 135 | output()->verbose( |
||||||||
| 136 | (new Format()) |
||||||||
| 137 | ->setContextualClass(Format::SUCCESS) |
||||||||
| 138 | ->setString(language()->getLine('CLI_REGISTRY_LANGUAGE_VERB_FETCH_MANIFEST_SUCCESS')) |
||||||||
| 139 | ->setIndent(2) |
||||||||
| 140 | ->setNewLinesAfter(1) |
||||||||
| 141 | ); |
||||||||
| 142 | } |
||||||||
| 143 | |||||||||
| 144 | $registry[ $package->getDirName() ] = $package; |
||||||||
| 145 | } elseif (is_cli()) { |
||||||||
| 146 | output()->verbose( |
||||||||
| 147 | (new Format()) |
||||||||
| 148 | ->setContextualClass(Format::DANGER) |
||||||||
| 149 | ->setString(language()->getLine('CLI_REGISTRY_LANGUAGE_VERB_FETCH_MANIFEST_FAILED')) |
||||||||
| 150 | ->setIndent(2) |
||||||||
| 151 | ->setNewLinesAfter(1) |
||||||||
| 152 | ); |
||||||||
| 153 | } |
||||||||
| 154 | } |
||||||||
| 155 | } |
||||||||
| 156 | } |
||||||||
| 157 | |||||||||
| 158 | ksort($registry); |
||||||||
| 159 | |||||||||
| 160 | return $registry; |
||||||||
| 161 | } |
||||||||
| 162 | |||||||||
| 163 | // ------------------------------------------------------------------------ |
||||||||
| 164 | |||||||||
| 165 | /** |
||||||||
| 166 | * Language::getDefaultMetadata |
||||||||
| 167 | * |
||||||||
| 168 | * @return array |
||||||||
| 169 | */ |
||||||||
| 170 | public function getDefaultMetadata() |
||||||||
| 171 | { |
||||||||
| 172 | return $this->getRegistry($this->getDefault()); |
||||||||
| 173 | } |
||||||||
| 174 | |||||||||
| 175 | // ------------------------------------------------------------------------ |
||||||||
| 176 | |||||||||
| 177 | /** |
||||||||
| 178 | * Language::getRegistry |
||||||||
| 179 | * |
||||||||
| 180 | * Gets language registries. |
||||||||
| 181 | * |
||||||||
| 182 | * @return array |
||||||||
| 183 | */ |
||||||||
| 184 | public function getRegistry($package = null) |
||||||||
| 185 | { |
||||||||
| 186 | if (isset($package)) { |
||||||||
| 187 | if ($this->registered($package)) { |
||||||||
| 188 | return $this->registry[ $package ]; |
||||||||
| 189 | } |
||||||||
| 190 | |||||||||
| 191 | return false; |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 192 | } |
||||||||
| 193 | |||||||||
| 194 | return $this->registry; |
||||||||
| 195 | } |
||||||||
| 196 | |||||||||
| 197 | // ------------------------------------------------------------------------ |
||||||||
| 198 | |||||||||
| 199 | /** |
||||||||
| 200 | * Language::registered |
||||||||
| 201 | * |
||||||||
| 202 | * @param $package |
||||||||
| 203 | * |
||||||||
| 204 | * @return bool |
||||||||
| 205 | */ |
||||||||
| 206 | public function registered($package) |
||||||||
| 207 | { |
||||||||
| 208 | return isset($this->registry[ $package ]); |
||||||||
| 209 | } |
||||||||
| 210 | |||||||||
| 211 | // ------------------------------------------------------------------------ |
||||||||
| 212 | |||||||||
| 213 | /** |
||||||||
| 214 | * Language::getTotalRegistry |
||||||||
| 215 | * |
||||||||
| 216 | * Gets num of registries. |
||||||||
| 217 | * |
||||||||
| 218 | * @return int |
||||||||
| 219 | */ |
||||||||
| 220 | public function getTotalRegistry() |
||||||||
| 221 | { |
||||||||
| 222 | return count($this->registry); |
||||||||
| 223 | } |
||||||||
| 224 | |||||||||
| 225 | // ------------------------------------------------------------------------ |
||||||||
| 226 | |||||||||
| 227 | /** |
||||||||
| 228 | * Language::updateRegistry |
||||||||
| 229 | * |
||||||||
| 230 | * Update language registry. |
||||||||
| 231 | * |
||||||||
| 232 | * @return void |
||||||||
| 233 | * @throws \Exception |
||||||||
| 234 | */ |
||||||||
| 235 | public function updateRegistry() |
||||||||
| 236 | { |
||||||||
| 237 | if (is_cli()) { |
||||||||
| 238 | output()->verbose( |
||||||||
| 239 | (new Format()) |
||||||||
| 240 | ->setContextualClass(Format::WARNING) |
||||||||
| 241 | ->setString(language()->getLine('CLI_REGISTRY_LANGUAGE_VERB_UPDATE_START')) |
||||||||
| 242 | ->setNewLinesBefore(1) |
||||||||
| 243 | ->setNewLinesAfter(2) |
||||||||
| 244 | ); |
||||||||
| 245 | } |
||||||||
| 246 | |||||||||
| 247 | $cacheItemPool = cache()->getObject('default'); |
||||||||
| 248 | |||||||||
| 249 | if (cache()->exists('registry')) { |
||||||||
| 250 | $cacheItemPool = cache()->getObject('registry'); |
||||||||
| 251 | } |
||||||||
| 252 | |||||||||
| 253 | if ($cacheItemPool instanceof CacheItemPoolInterface) { |
||||||||
| 254 | $this->registry = $this->fetchRegistry(); |
||||||||
| 255 | $cacheItemPool->save(new Item('o2languages', $this->registry, false)); |
||||||||
|
0 ignored issues
–
show
false of type false is incompatible with the type DateInterval|integer expected by parameter $expiresAfter of O2System\Cache\Item::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 256 | } |
||||||||
| 257 | |||||||||
| 258 | if (count($this->registry) and is_cli()) { |
||||||||
| 259 | output()->verbose( |
||||||||
| 260 | (new Format()) |
||||||||
| 261 | ->setContextualClass(Format::SUCCESS) |
||||||||
| 262 | ->setString(language()->getLine('CLI_REGISTRY_LANGUAGE_VERB_UPDATE_SUCCESS')) |
||||||||
| 263 | ->setNewLinesBefore(1) |
||||||||
| 264 | ->setNewLinesAfter(2) |
||||||||
| 265 | ); |
||||||||
| 266 | } elseif (is_cli()) { |
||||||||
| 267 | output()->verbose( |
||||||||
| 268 | (new Format()) |
||||||||
| 269 | ->setContextualClass(Format::DANGER) |
||||||||
| 270 | ->setString(language()->getLine('CLI_REGISTRY_LANGUAGE_VERB_UPDATE_FAILED')) |
||||||||
| 271 | ->setNewLinesBefore(1) |
||||||||
| 272 | ->setNewLinesAfter(2) |
||||||||
| 273 | ); |
||||||||
| 274 | } |
||||||||
| 275 | } |
||||||||
| 276 | |||||||||
| 277 | // ------------------------------------------------------------------------ |
||||||||
| 278 | |||||||||
| 279 | /** |
||||||||
| 280 | * Language::flushRegistry |
||||||||
| 281 | * |
||||||||
| 282 | * Flush language registry. |
||||||||
| 283 | * |
||||||||
| 284 | * @return void |
||||||||
| 285 | * @throws \Psr\Cache\InvalidArgumentException |
||||||||
| 286 | */ |
||||||||
| 287 | public function flushRegistry() |
||||||||
| 288 | { |
||||||||
| 289 | $cacheItemPool = cache()->getItemPool('default'); |
||||||||
| 290 | |||||||||
| 291 | if (cache()->exists('registry')) { |
||||||||
| 292 | $cacheItemPool = cache()->getItemPool('registry'); |
||||||||
| 293 | } |
||||||||
| 294 | |||||||||
| 295 | if ($cacheItemPool instanceof CacheItemPoolInterface) { |
||||||||
| 296 | $cacheItemPool->deleteItem('o2languages'); |
||||||||
| 297 | } |
||||||||
| 298 | } |
||||||||
| 299 | } |