'how to add code to service provider by artisan command
i created an Artisan Command that created Repository. A Repository File and an Interface. when user run below command, this files will be generated.
php artisan make:repository RepositoryName
now, next step is to add bind code to RepositoryServiceProvider in register method. How do I add the following code to that file?
$this->app->bind(
RepositoryNameInterface::class,
RepositoryName::class,
);
and generally, how to add custom code to the method of class in PHP?
Solution 1:[1]
You can keep the repositories interface as key and repository as the value in an array in a file.
First, add use Illuminate\Filesystem\Filesystem; and initialize it in the make:repository command constructor like :
protected $files;
public function __construct( Filesystem $files)
{
parent::__construct();
$this->files = $files;
}
Let's add a function in your make:repository command.
// Add a function in make:repository command
public function writeToRepoCacheFile($repoName, $repoInterface)
{
// Let's make sure cache folder exists
$cacheDirPath = base_path('bootstrap') . DIRECTORY_SEPARATOR . 'cache';
$this->files->ensureDirectoryExists($cacheDirPath);
// The full path we will be keeping the array of repos
$file = $cacheDirPath . DIRECTORY_SEPARATOR . 'repositories.php';
if (! is_writable(dirname($file))) {
throw new Exception('The '.dirname($file).' directory must be present and writable.');
}
// Merge with the previously added repo's if available
$repositories = [];
if ($this->files->exists($file)) {
$repositories = $this->files->getRequire($file);
}
$repositoryList = array_merge($repositories, [
$repoInterface => $repoName
]);
$this->files->replace($file, '<?php'. PHP_EOL . PHP_EOL .'return ' . var_export($repositoryList, true).';');
}
Now, whenever you create a new repo, the writeToRepoCacheFile method should be called with parameters of $repoName and $interface with their respective path. The method will simply create a cache file in our bootstrap/cache folder naming repositories and add your recently added repos as an array in the file which later will be called in our service provider to get register.
The final one is to bind the interface with our repository.
public function bindTheRepos()
{
$repositories = base_path('bootstrap/cache/repositories.php');
if ($this->files->exists($repositories)) {
Collection::make($this->files->getRequire($repositories))
->map(function ($repo, $repoInterface) {
// Make sure the files exists
// Interface exists
$interfaceExists = $this->app['files']->exists(
$repoInterface.'.php'
);
// Repo exists
$repoExists = $this->app['files']->exists(
$repo.'.php'
);
if (
$interfaceExists &&
$repoExists
))) {
$this->app->bind($repoInterface, $repo);
}
});
}
}
Add the above function in your service provider and call the function in the register method of the service provider.
Make sure the interface and repository classes path matches your structured path.
This will dynamically bind your newly created repository with its interface.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
