|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Lauthz\Loaders; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use Illuminate\Support\Manager; |
| 8 | +use InvalidArgumentException; |
| 9 | + |
| 10 | +/** |
| 11 | + * The model loader manager. |
| 12 | + * |
| 13 | + * A model loader is responsible for a loading model from an arbitrary source. |
| 14 | + * Developers can customize loading behavior by implementing |
| 15 | + * and register the custom loader in AppServiceProvider through `app(LoaderManager::class)->extend()`. |
| 16 | + * |
| 17 | + * Built-in loader implementations include: |
| 18 | + * - FileLoader: For loading model from file. |
| 19 | + * - TextLoader: Suitable for model defined as a multi-line string. |
| 20 | + * - UrlLoader: Handles model loading from URL. |
| 21 | + * |
| 22 | + * To utilize a built-in or custom loader, set 'model.config_type' in the configuration to match one of the above types. |
| 23 | + */ |
| 24 | +class ModelLoaderManager extends Manager |
| 25 | +{ |
| 26 | + |
| 27 | + /** |
| 28 | + * The array of the lauthz driver configuration. |
| 29 | + * |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + protected $config; |
| 33 | + |
| 34 | + /** |
| 35 | + * Initialize configuration for the loader manager instance. |
| 36 | + * |
| 37 | + * @param array $config the lauthz driver configuration. |
| 38 | + */ |
| 39 | + public function initFromConfig(array $config) |
| 40 | + { |
| 41 | + $this->config = $config; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Get the default driver from the configuration. |
| 46 | + * |
| 47 | + * @return string The default driver name. |
| 48 | + */ |
| 49 | + public function getDefaultDriver() |
| 50 | + { |
| 51 | + return Arr::get($this->config, 'model.config_type', ''); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a new TextLoader instance. |
| 56 | + * |
| 57 | + * @return TextLoader |
| 58 | + */ |
| 59 | + public function createTextDriver() |
| 60 | + { |
| 61 | + return new TextLoader($this->config); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Create a new UrlLoader instance. |
| 66 | + * |
| 67 | + * @return UrlLoader |
| 68 | + */ |
| 69 | + public function createUrlDriver() |
| 70 | + { |
| 71 | + return new UrlLoader($this->config); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Create a new FileLoader instance. |
| 76 | + * |
| 77 | + * @return FileLoader |
| 78 | + */ |
| 79 | + public function createFileDriver() |
| 80 | + { |
| 81 | + return new FileLoader($this->config); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Create a new driver instance. |
| 86 | + * |
| 87 | + * @param string $driver |
| 88 | + * @return mixed |
| 89 | + * |
| 90 | + * @throws \InvalidArgumentException |
| 91 | + */ |
| 92 | + protected function createDriver($driver) |
| 93 | + { |
| 94 | + if(empty($driver)) { |
| 95 | + throw new InvalidArgumentException('Unsupported empty model loader type.'); |
| 96 | + } |
| 97 | + |
| 98 | + if (isset($this->customCreators[$driver])) { |
| 99 | + return $this->callCustomCreator($driver); |
| 100 | + } |
| 101 | + $method = 'create' . Str::studly($driver) . 'Driver'; |
| 102 | + if (method_exists($this, $method)) { |
| 103 | + return $this->$method(); |
| 104 | + } |
| 105 | + |
| 106 | + throw new InvalidArgumentException("Unsupported model loader type: {$driver}."); |
| 107 | + } |
| 108 | +} |
0 commit comments