Skip to content

Commit

Permalink
Sample of implementing an IoC container for dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigori Kochanov committed Dec 5, 2020
1 parent 7d4d379 commit e5cfc5c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -2,7 +2,9 @@
"name": "grikdotnet/monolith-decomposition", "name": "grikdotnet/monolith-decomposition",
"type": "project", "type": "project",
"require": { "require": {
"php": "^7.4" "php": "^8.0",
"symfony/dependency-injection": "^5.2",
"symfony/config": "^5.2"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
Expand Down
26 changes: 23 additions & 3 deletions public/index.php
@@ -1,7 +1,27 @@
<?php <?php


use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

include '../vendor/autoload.php'; include '../vendor/autoload.php';


// Usually an application kernel and is called here, // Usually an application kernel creates
// it calls routing, then some middleware, and then controllers // an IoC-container, routing, some middleware, and controllers,
new Acme\ImageResizeController(); // but I'll just create a container and a controller drectly.

$containerBuilder = new ContainerBuilder();
$loader = new PhpFileLoader($containerBuilder, new FileLocator('..'));
$loader->load('services.php');
$containerBuilder->compile();
// in a real project there will be some cacheing of the container
// file_put_contents($file, (new PhpDumper($containerBuilder))->dump());

try {
$controller = $containerBuilder->get(\Acme\ImageResizeController::class);
} catch (Exception $e) {
echo $e->getMessage(),"\n",$e->getTraceAsString();
exit;
}

$controller->action($_GET['id']?:null);
14 changes: 14 additions & 0 deletions services.php
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return function(ContainerConfigurator $configurator) {
/**
* @see https://symfony.com/doc/current/service_container/autowiring.html
*/
$services = $configurator->services()->defaults()->autowire()->public();

$services->load('Acme\\', './src/*')
->exclude('./src/BaseModel.php');

};
3 changes: 2 additions & 1 deletion src/Image.php
Expand Up @@ -8,7 +8,8 @@
*/ */
class Image extends \BaseModel class Image extends \BaseModel
{ {
public function __construct($filename)
public function load(mixed $tmp_name)
{ {
// calls to inherited methods // calls to inherited methods
$this->foo(); $this->foo();
Expand Down
10 changes: 7 additions & 3 deletions src/ImageResizeController.php
Expand Up @@ -6,9 +6,13 @@


class ImageResizeController class ImageResizeController
{ {
public function __construct() public function __construct(
public Image $image
){
}

public function action(?string $id)
{ {
$Image = new Image($_GET['id']?:null); $this->image->load($id);
// other code skipped ...
} }
} }

0 comments on commit e5cfc5c

Please sign in to comment.