commit 1fc7fd5553b0b8f6e278c2c0eca9ca55ebc3f858 Author: a-sansara Date: Mon Mar 13 18:07:46 2017 +0100 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d9b7477 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..df68a0c --- /dev/null +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9e3f712 --- /dev/null +++ b/composer.json @@ -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" + } +} diff --git a/src/MetaTech/Silex/Provider/ControllerServiceProvider.php b/src/MetaTech/Silex/Provider/ControllerServiceProvider.php new file mode 100644 index 0000000..a66dc60 --- /dev/null +++ b/src/MetaTech/Silex/Provider/ControllerServiceProvider.php @@ -0,0 +1,69 @@ +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)); + } + } + } + } +}