-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5cbafdb
Showing
5 changed files
with
140 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor | ||
/composer.lock |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Kekalainen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,33 @@ | ||
# Slim Illuminate autowire | ||
|
||
Autowire class dependencies using the Laravel/Illuminate [service container](https://laravel.com/docs/9.x/container) by proxying Slim's [callable resolver](https://www.slimframework.com/docs/v4/objects/routing.html#container-resolution). | ||
|
||
## Usage | ||
|
||
```php | ||
use Illuminate\Container\Container; | ||
use Illuminate\Contracts\Container\Container as ContainerContract; | ||
use Kekalainen\SlimIlluminateAutowire\AdvancedCallableResolverProxy; | ||
use Slim\CallableResolver; | ||
use Slim\Factory\AppFactory; | ||
use Slim\Interfaces\AdvancedCallableResolverInterface; | ||
|
||
// Instantiate the Illuminate container. | ||
$container = new Container(); | ||
|
||
// Bind the container instance into the container. | ||
$container->instance(ContainerContract::class, $container); | ||
|
||
// Bind a concrete advanced callable resolver to be proxied. | ||
$container->bind(AdvancedCallableResolverInterface::class, CallableResolver::class); | ||
|
||
// Resolve the proxied callable resolver. | ||
$callableResolver = $container->make(AdvancedCallableResolverProxy::class); | ||
|
||
// Set the static properties of the Slim App factory. | ||
AppFactory::setContainer($container); | ||
AppFactory::setCallableResolver($callableResolver); | ||
|
||
// Instantiate the Slim App. | ||
$app = AppFactory::create(); | ||
``` |
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,20 @@ | ||
{ | ||
"name": "kekalainen/slim-illuminate-autowire", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Kekalainen", | ||
"email": "git@kekalainen.me" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Kekalainen\\SlimIlluminateAutowire\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"illuminate/container": ">=4.0", | ||
"slim/slim": ">=4.0" | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace Kekalainen\SlimIlluminateAutowire; | ||
|
||
use Illuminate\Contracts\Container\Container; | ||
use Slim\Interfaces\AdvancedCallableResolverInterface; | ||
|
||
/** | ||
* Proxies an advanced callable resolver to provide (factoryless) automatic dependency injection based on type declarations. | ||
* | ||
* https://www.slimframework.com/docs/v4/objects/routing.html#registering-a-controller-with-the-container | ||
* https://laravel.com/docs/9.x/container#automatic-injection | ||
*/ | ||
class AdvancedCallableResolverProxy implements AdvancedCallableResolverInterface | ||
{ | ||
protected Container $container; | ||
|
||
protected AdvancedCallableResolverInterface $callableResolver; | ||
|
||
public function __construct(Container $container, AdvancedCallableResolverInterface $callableResolver) | ||
{ | ||
$this->container = $container; | ||
$this->callableResolver = $callableResolver; | ||
} | ||
|
||
/** | ||
* Prepare $toResolve for container resolution. | ||
*/ | ||
protected function prepareToResolve($toResolve): void | ||
{ | ||
if (!is_string($toResolve)) | ||
return; | ||
|
||
// Extract the class name from Slim notation. | ||
[$class] = explode(':', $toResolve); | ||
|
||
if ( | ||
$this->container->has($class) || | ||
!class_exists($class) | ||
) | ||
return; | ||
|
||
// Resolve the class instance (including any type-hinted dependencies) and bind it into the container. | ||
$this->container->instance($class, $this->container->make($class)); | ||
} | ||
|
||
public function resolve($toResolve): callable | ||
{ | ||
$this->prepareToResolve($toResolve); | ||
return $this->callableResolver->resolve($toResolve); | ||
} | ||
|
||
public function resolveRoute($toResolve): callable | ||
{ | ||
$this->prepareToResolve($toResolve); | ||
return $this->callableResolver->resolveRoute($toResolve); | ||
} | ||
|
||
public function resolveMiddleware($toResolve): callable | ||
{ | ||
$this->prepareToResolve($toResolve); | ||
return $this->callableResolver->resolveMiddleware($toResolve); | ||
} | ||
} |