Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample of implementing an IoC container for dependency injection
- Loading branch information
Grigori Kochanov
committed
Dec 5, 2020
1 parent
7d4d379
commit e5cfc5c
Showing
5 changed files
with
49 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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'); | |||
|
|||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters