-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use Laravel's built-in Manager class
- Use Laravel's built-in abstract Manager class instead of ModelLoaderFactory (#71)
- Loading branch information
Showing
7 changed files
with
149 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Lauthz\Loaders; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Support\Manager; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* The model loader manager. | ||
* | ||
* A model loader is responsible for a loading model from an arbitrary source. | ||
* Developers can customize loading behavior by implementing | ||
* and register the custom loader in AppServiceProvider through `app(LoaderManager::class)->extend()`. | ||
* | ||
* Built-in loader implementations include: | ||
* - FileLoader: For loading model from file. | ||
* - TextLoader: Suitable for model defined as a multi-line string. | ||
* - UrlLoader: Handles model loading from URL. | ||
* | ||
* To utilize a built-in or custom loader, set 'model.config_type' in the configuration to match one of the above types. | ||
*/ | ||
class ModelLoaderManager extends Manager | ||
{ | ||
|
||
/** | ||
* The array of the lauthz driver configuration. | ||
* | ||
* @var array | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* Initialize configuration for the loader manager instance. | ||
* | ||
* @param array $config the lauthz driver configuration. | ||
*/ | ||
public function initFromConfig(array $config) | ||
{ | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Get the default driver from the configuration. | ||
* | ||
* @return string The default driver name. | ||
*/ | ||
public function getDefaultDriver() | ||
{ | ||
return Arr::get($this->config, 'model.config_type', ''); | ||
} | ||
|
||
/** | ||
* Create a new TextLoader instance. | ||
* | ||
* @return TextLoader | ||
*/ | ||
public function createTextDriver() | ||
{ | ||
return new TextLoader($this->config); | ||
} | ||
|
||
/** | ||
* Create a new UrlLoader instance. | ||
* | ||
* @return UrlLoader | ||
*/ | ||
public function createUrlDriver() | ||
{ | ||
return new UrlLoader($this->config); | ||
} | ||
|
||
/** | ||
* Create a new FileLoader instance. | ||
* | ||
* @return FileLoader | ||
*/ | ||
public function createFileDriver() | ||
{ | ||
return new FileLoader($this->config); | ||
} | ||
|
||
/** | ||
* Create a new driver instance. | ||
* | ||
* @param string $driver | ||
* @return mixed | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
protected function createDriver($driver) | ||
{ | ||
if(empty($driver)) { | ||
throw new InvalidArgumentException('Unsupported empty model loader type.'); | ||
} | ||
|
||
if (isset($this->customCreators[$driver])) { | ||
return $this->callCustomCreator($driver); | ||
} | ||
$method = 'create' . Str::studly($driver) . 'Driver'; | ||
if (method_exists($this, $method)) { | ||
return $this->$method(); | ||
} | ||
|
||
throw new InvalidArgumentException("Unsupported model loader type: {$driver}."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters