Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kekalainen committed Nov 27, 2022
0 parents commit 5cbafdb
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/composer.lock
21 changes: 21 additions & 0 deletions LICENSE
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.
33 changes: 33 additions & 0 deletions README.md
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();
```
20 changes: 20 additions & 0 deletions composer.json
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"
}
}
64 changes: 64 additions & 0 deletions src/AdvancedCallableResolverProxy.php
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);
}
}

0 comments on commit 5cbafdb

Please sign in to comment.