-
Notifications
You must be signed in to change notification settings - Fork 52
feat: use Laravel's built-in Manager class #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.