|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codenom\Assets\Handlers; |
| 4 | + |
| 5 | +use CodeIgniter\Config\BaseConfig; |
| 6 | +use Config\Services; |
| 7 | +use Codenom\Assets\Exceptions\AssetsException; |
| 8 | +use Codenom\Assets\Handlers\DirectoryHandler; |
| 9 | +use Codenom\Assets\Interfaces\AssetHandlerInterface; |
| 10 | + |
| 11 | +class ConfigHandler implements AssetHandlerInterface |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Instance of the directory handler for config routes |
| 15 | + * that point to directories instead of files. |
| 16 | + * |
| 17 | + * @var \Codenom\Assets\Handlers\DirectoryHandler |
| 18 | + */ |
| 19 | + protected $directoryHandler; |
| 20 | + |
| 21 | + // Save the config |
| 22 | + public function __construct(BaseConfig $config = null) |
| 23 | + { |
| 24 | + // Save the configuration |
| 25 | + $this->config = $config ?? config('Assets'); |
| 26 | + } |
| 27 | + |
| 28 | + // Search the config property for each segment |
| 29 | + public function gather(string $route): array |
| 30 | + { |
| 31 | + $tmpRoute = ''; |
| 32 | + $paths = $this->gatherFromConfigRoute($tmpRoute); |
| 33 | + |
| 34 | + foreach (explode('/', $route) as $segment) { |
| 35 | + $tmpRoute = empty($tmpRoute) ? $segment : $tmpRoute . '/' . $segment; |
| 36 | + $paths = array_merge($paths, $this->gatherFromConfigRoute($tmpRoute)); |
| 37 | + } |
| 38 | + |
| 39 | + return $paths; |
| 40 | + } |
| 41 | + |
| 42 | + // Gather asset files from a single config route |
| 43 | + protected function gatherFromConfigRoute(string $route): array |
| 44 | + { |
| 45 | + if (!isset($this->config->routes[$route])) { |
| 46 | + return []; |
| 47 | + } |
| 48 | + |
| 49 | + $paths = []; |
| 50 | + foreach ($this->config->routes[$route] as $item) { |
| 51 | + $extension = strtolower(pathinfo($item, PATHINFO_EXTENSION)); |
| 52 | + |
| 53 | + // Check empty extensions for a valid directory |
| 54 | + if (empty($extension)) { |
| 55 | + $directory = rtrim($this->config->fileBase, '/') . '/' . $item; |
| 56 | + if (is_dir($directory)) { |
| 57 | + $paths = array_merge($paths, $this->gatherFromDirectory($directory)); |
| 58 | + } elseif (!$this->config->silent) { |
| 59 | + throw AssetsException::forInvalidConfigItem($item); |
| 60 | + } else { |
| 61 | + log_message('warning', lang('Assets.invalidConfigItem', [$item])); |
| 62 | + } |
| 63 | + } elseif (in_array($extension, $this->config->extensions)) { |
| 64 | + $paths[] = $item; |
| 65 | + } elseif (!$this->config->silent) { |
| 66 | + throw AssetsException::forUnsupportedExtension($extension); |
| 67 | + } else { |
| 68 | + log_message('warning', lang('Assets.unsupportedExtension', [$extension])); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return $paths; |
| 73 | + } |
| 74 | + |
| 75 | + // Load and call the directory handler |
| 76 | + protected function gatherFromDirectory(string $directory): array |
| 77 | + { |
| 78 | + if (is_null($this->directoryHandler)) { |
| 79 | + $this->directoryHandler = new DirectoryHandler($this->config); |
| 80 | + } |
| 81 | + |
| 82 | + return $this->directoryHandler->gatherFromDirectory($directory); |
| 83 | + } |
| 84 | +} |
0 commit comments