initial commit

This commit is contained in:
a-sansara 2017-03-13 18:07:46 +01:00
commit 1fc7fd5553
4 changed files with 203 additions and 0 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 meta-tech.academy
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.

90
README.md Normal file
View File

@ -0,0 +1,90 @@
# MetaTech\Silex\ControllerServiceProvider
A service provider for [ Silex ](http://silex.sensiolabs.org) for managing DI and mounting on controllers.
### Requirements
PHP 7.0
### Install
The package can be installed using [ Composer ](https://getcomposer.org/). (not yet)
```
composer require meta-tech/silex-controller-service
```
Or add the package to your `composer.json`.
```
"require": {
"meta-tech/silex-controller-service" : "1.0"
}
```
## Usage
The provider will create a service relative to a Controller instance builded with its dependencies.
If the controller implements the `Silex\Api\ControllerProviderInterface` the provider will also
mount the controller 's routes to the defined routing entry point
## Example
Admit you have a controller Test with dependencies on instanciation :
```php
class Test implements ControllerProviderInterface
{
public function __construct(Application $app)
{
// do stuff
}
```
You can use the ControllerServiceProvider to create a service to manage your
controller class instanciation :
```php
use MetaTech\Silex\Provider\ControllerServiceProvider;
use Acme\Ctrl\Test;
...
$app->register(new ControllerServiceProvider(Test::class, [$app], '/test', 'ctrl.'));
```
**first parameter** is your *controller class*
**second parameter** is an array of your *controller depencies*
**third parameter** define your controller *routing entry point*
**fouth parameter** define your *service 's namespace* to acces your controller (default ctrl.)
the name of the registering service is the *given namespace* followed by your *controller class shortname*
with the previous example `$app['ctrl.Test']` is now available and return your controller instance.
the `connect` method of your controller can now benefits of this service to define appropriate routes, like that :
```php
class Test implements ControllerProviderInterface
{
...
public function connect(Application $app)
{
$collection = $app['controllers_factory'];
$_ = 'ctrl.Test';
$collection->match('/' , "$_:index");
$collection->match('/test', "$_:test");
return $collection;
}
}
```
see source code of `MetaTech\Core\Ws` for an advance controller architecture.
### License
The project is released under the MIT license, see the LICENSE file.

23
composer.json Normal file
View File

@ -0,0 +1,23 @@
{
"name" : "meta-tech/silex-controller-service",
"type" : "library",
"homepage" : "https://github.com/meta-tech/silex-controller-service",
"description" : "Controller service for Silex.",
"license" : "MIT",
"authors" : [
{
"name" : "a-Sansara",
"homepage" : "https://github.com/a-sansara/"
}
],
"keywords" : ["Silex", "Controller", "Service", "Provider"],
"autoload" : {
"psr-4" : {
"" : "src/"
}
},
"require" : {
"php" : "^7.0",
"pimple/pimple" : "~3.0"
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace MetaTech\Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/*!
* @package MetaTech\Silex\Provider
* @class ControllerServiceProvider
* @implements Pimple\ServiceProviderInterface
* @author a-Sansara
* @date 2017-03-13 16:40:56 CET
*/
class ControllerServiceProvider implements ServiceProviderInterface
{
/*! @private @var str $name */
private $name;
/*! @private @var str $ns */
private $ns;
/*! @private @var str $route */
private $route;
/*! @private @var [mixed] $args */
private $args;
/*
* ControllerServiceProvider constructor
*
* @constructor
* @public
* @param str $class a controller class
* @param [mixed] $args controller arguments
* @param str $route controller entry point
* @param str $namespace controller service namespace in application
*/
public function __construct($class, $args=[], $route=null, $namespace='ctrl.')
{
if (class_exists($class)) {
$this->name = $class;
$this->ns = $namespace . (new \ReflectionClass($class))->getShortName();
$this->route = $route;
$this->args = $args;
}
}
/*!
* create a service dedicated to the controller and mount the controller's routes
*
* @method register
* @public
* @param Pimple\Container $app
*/
public function register(Container $app)
{
if (!is_null($this->name)) {
$class = $this->name;
$args = $this->args;
$app[$this->ns] = function() use ($class, $args) {
return new $class(...$args);
};
if (!is_null($this->route)) {
$imp = class_implements($class);
if (isset($imp['Silex\\Api\\ControllerProviderInterface'])) {
$app->mount($this->route, $app[$this->ns]->connect($app));
}
}
}
}
}