From 7cc2d4066088fbd6b829a24dceaa2567b1ee8c37 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Sun, 28 Jun 2020 19:26:59 +0200 Subject: [PATCH] add Dependencies and Routes classes --- composer.json | 3 +- composer.lock | 215 ++++++++++++++++++++++++++++++++++- public/index.php | 15 +-- src/Dependencies.php | 29 +++++ src/HelloWorldController.php | 23 ++++ src/Routes.php | 15 +++ 6 files changed, 288 insertions(+), 12 deletions(-) create mode 100644 src/Dependencies.php create mode 100644 src/HelloWorldController.php create mode 100644 src/Routes.php diff --git a/composer.json b/composer.json index d14d0b4..34cd9b3 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "require": { "php": "^7.4", "slim/slim": "^4.5", - "slim/psr7": "^1.1" + "slim/psr7": "^1.1", + "php-di/php-di": "^6.2" }, "require-dev": { "phpunit/phpunit": "^9" diff --git a/composer.lock b/composer.lock index 7a2270e..8ded0f0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c94e0dc94538087f6c99d168389da841", + "content-hash": "c9b48ebdf86b04852bf99bd1932d57e8", "packages": [ { "name": "fig/http-message-util", @@ -104,6 +104,219 @@ ], "time": "2018-02-13T20:26:39+00:00" }, + { + "name": "opis/closure", + "version": "3.5.5", + "source": { + "type": "git", + "url": "https://github.com/opis/closure.git", + "reference": "dec9fc5ecfca93f45cd6121f8e6f14457dff372c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/opis/closure/zipball/dec9fc5ecfca93f45cd6121f8e6f14457dff372c", + "reference": "dec9fc5ecfca93f45cd6121f8e6f14457dff372c", + "shasum": "" + }, + "require": { + "php": "^5.4 || ^7.0" + }, + "require-dev": { + "jeremeamia/superclosure": "^2.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.5.x-dev" + } + }, + "autoload": { + "psr-4": { + "Opis\\Closure\\": "src/" + }, + "files": [ + "functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marius Sarca", + "email": "marius.sarca@gmail.com" + }, + { + "name": "Sorin Sarca", + "email": "sarca_sorin@hotmail.com" + } + ], + "description": "A library that can be used to serialize closures (anonymous functions) and arbitrary objects.", + "homepage": "https://opis.io/closure", + "keywords": [ + "anonymous functions", + "closure", + "function", + "serializable", + "serialization", + "serialize" + ], + "time": "2020-06-17T14:59:55+00:00" + }, + { + "name": "php-di/invoker", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/PHP-DI/Invoker.git", + "reference": "540c27c86f663e20fe39a24cd72fa76cdb21d41a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHP-DI/Invoker/zipball/540c27c86f663e20fe39a24cd72fa76cdb21d41a", + "reference": "540c27c86f663e20fe39a24cd72fa76cdb21d41a", + "shasum": "" + }, + "require": { + "psr/container": "~1.0" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "phpunit/phpunit": "~4.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Invoker\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Generic and extensible callable invoker", + "homepage": "https://github.com/PHP-DI/Invoker", + "keywords": [ + "callable", + "dependency", + "dependency-injection", + "injection", + "invoke", + "invoker" + ], + "time": "2017-03-20T19:28:22+00:00" + }, + { + "name": "php-di/php-di", + "version": "6.2.1", + "source": { + "type": "git", + "url": "https://github.com/PHP-DI/PHP-DI.git", + "reference": "6875fe557c244b3830862c072c7719ca4ac2efe4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHP-DI/PHP-DI/zipball/6875fe557c244b3830862c072c7719ca4ac2efe4", + "reference": "6875fe557c244b3830862c072c7719ca4ac2efe4", + "shasum": "" + }, + "require": { + "opis/closure": "^3.5.5", + "php": ">=7.2.0", + "php-di/invoker": "^2.0", + "php-di/phpdoc-reader": "^2.0.1", + "psr/container": "^1.0" + }, + "provide": { + "psr/container-implementation": "^1.0" + }, + "require-dev": { + "doctrine/annotations": "~1.2", + "friendsofphp/php-cs-fixer": "^2.4", + "mnapoli/phpunit-easymock": "^1.2", + "ocramius/proxy-manager": "~2.0.2", + "phpstan/phpstan": "^0.12", + "phpunit/phpunit": "^8.5" + }, + "suggest": { + "doctrine/annotations": "Install it if you want to use annotations (version ~1.2)", + "ocramius/proxy-manager": "Install it if you want to use lazy injection (version ~2.0)" + }, + "type": "library", + "autoload": { + "psr-4": { + "DI\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "The dependency injection container for humans", + "homepage": "https://php-di.org/", + "keywords": [ + "PSR-11", + "container", + "container-interop", + "dependency injection", + "di", + "ioc", + "psr11" + ], + "funding": [ + { + "url": "https://github.com/mnapoli", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/php-di/php-di", + "type": "tidelift" + } + ], + "time": "2020-06-18T09:54:32+00:00" + }, + { + "name": "php-di/phpdoc-reader", + "version": "2.1.1", + "source": { + "type": "git", + "url": "https://github.com/PHP-DI/PhpDocReader.git", + "reference": "15678f7451c020226807f520efb867ad26fbbfcf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHP-DI/PhpDocReader/zipball/15678f7451c020226807f520efb867ad26fbbfcf", + "reference": "15678f7451c020226807f520efb867ad26fbbfcf", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "PhpDocReader\\": "src/PhpDocReader" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PhpDocReader parses @var and @param values in PHP docblocks (supports namespaced class names with the same resolution rules as PHP)", + "keywords": [ + "phpdoc", + "reflection" + ], + "time": "2019-09-26T11:24:58+00:00" + }, { "name": "psr/container", "version": "1.0.0", diff --git a/public/index.php b/public/index.php index 711e3d1..e5121cd 100644 --- a/public/index.php +++ b/public/index.php @@ -3,18 +3,13 @@ declare(strict_types=1); require_once __DIR__ . '/../vendor/autoload.php'; -use Psr\Http\Message\ResponseInterface as Response; -use Psr\Http\Message\ServerRequestInterface as Request; +use NoteCat\Dependencies; +use NoteCat\Routes; use Slim\Factory\AppFactory; -use NoteCat\HelloWorld; +$container = Dependencies::createContainer(); +$app = AppFactory::createFromContainer($container); -$app = AppFactory::create(); - -$app->get('/', function (Request $request, Response $response, $args) { - $hello = new HelloWorld(); - $response->getBody()->write('' . $hello->getHello() . "\n"); - return $response; -}); +Routes::setRoutes($app); $app->run(); diff --git a/src/Dependencies.php b/src/Dependencies.php new file mode 100644 index 0000000..b0a6172 --- /dev/null +++ b/src/Dependencies.php @@ -0,0 +1,29 @@ +set(HelloWorldController::class, function (ContainerInterface $c) { + return new HelloWorldController( + $c->get(HelloWorld::class) + ); + }); + + // Services + $container->set(HelloWorld::class, function () { + return new HelloWorld(); + }); + + return $container; + } +} diff --git a/src/HelloWorldController.php b/src/HelloWorldController.php new file mode 100644 index 0000000..b98ba33 --- /dev/null +++ b/src/HelloWorldController.php @@ -0,0 +1,23 @@ +hello = $hello; + } + + public function hello(Request $request, Response $response): Response + { + $response->getBody()->write('' . $this->hello->getHello() . "\n"); + return $response; + } +} diff --git a/src/Routes.php b/src/Routes.php new file mode 100644 index 0000000..d47f2c6 --- /dev/null +++ b/src/Routes.php @@ -0,0 +1,15 @@ +get('/', HelloWorldController::class . ':hello'); + } +}