PHP Classes

File: diContainer.php

Recommend this page to a friend!
  Packages of Vitalij Mik   Attributes   diContainer.php   Download  
File: diContainer.php
Role: Configuration script
Content type: text/plain
Description: Route configuration script
Class: Attributes
Route requests based on the current URL and method
Author: By
Last change:
Date: 1 year ago
Size: 883 bytes
 

Contents

Class file image Download
<?php
$container = [];


$container[IndexController::class] = function () {
    return new IndexController();
};
$container[ProfileController::class] = function () {
    return new ProfileController();
};
$container[Router::class] = function () use ($container) {
    $router = new Router();

    foreach($container as $className => $service){
        $reflectionClass = new ReflectionClass($className);
        $methods = $reflectionClass->getMethods();
        foreach($methods as $method){
            $attributes = $method->getAttributes(Route::class);
            foreach($attributes as $attribute){
                /** @var Route $route */
                $route = $attribute->newInstance();
                $route->setAction([$service(),$method->getName()]);

                $router->register($route);
            }
        }
    }

    return $router;
};

return $container;