超薄DI容器

时间:2016-08-11 09:54:06

标签: php oop dependency-injection slim

我的Slim 3应用程序索引结构是:

// Instantiate the app
$config = require __DIR__ . '/../src/config.php';
$app = new \Slim\App($config);

// Set up dependencies
require __DIR__ . '/../src/dependencies.php';

// Register middleware
require __DIR__ . '/../src/middleware.php';

// Register routes
require __DIR__ . '/../src/routes.php';

// Run app
$app->run();

我有单独的worker.php文件,该文件通过bash脚本执行,此文件在后台不间断运行(侦听某些事件)。

如何在我的worker.php文件中注入服务,该文件不属于Slim应用程序或者来自dependencies.php的$ container,例如我想使用Monolog记录器。 依赖关系:

$container = $app->getContainer();

/**@var \App\model\Model access to Model methods */
$model = new \App\model\Loan($app->getContainer()->get('settings')['db']);

$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($settings['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], Monolog\Logger::DEBUG));
    return $logger;
};

3 个答案:

答案 0 :(得分:2)

最简单的方法是在worker.php中使用相同的DI容器

参考: https://github.com/slimphp/Slim/blob/3.x/Slim/Container.php

$config = require __DIR__ . '/../src/config.php';
$container = new \Slim\Container($config);
require __DIR__ . '/../src/dependencies.php';

答案 1 :(得分:0)

您的设置

return [
    'settings' => [
        'displayErrorDetails' => true, 
        // 'addContentLengthHeader' => false, // Allow the web server to send the content-length header

        // Monolog settings
        'logger' => [
            'name' => 'slim-app',
            'path' => __DIR__ . '/../logs/app.log',
            'level' => \Monolog\Logger::DEBUG,
        ],

    ],
];

在你的依赖中。

$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($settings['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], Monolog\Logger::DEBUG));
    return $logger;
  };

答案 2 :(得分:0)

您可以使用Monolog-Cascade完成此操作,如下所示:

在依赖项中:

// Configure Monolog with Cascade
$container['logger'] = function ($c) {
    Cascade::loadConfigFromArray($c->get('settings')['loggerConfig']);
    return Cascade::logger('logger_name_here');
};

然后在配置文件中(带有2个处理程序的示例):

// Monolog-Cascade settings
    'loggerConfig' => [
        'handlers' => [
            // Stdout handler
            'console' => [
                'class' => 'Monolog\Handler\StreamHandler',
                'stream' => 'php://stdout',
                'level' => 'DEBUG'
            ],
            // File handler
            'file' => [
                'class' => 'Monolog\Handler\StreamHandler',
                'stream' => __DIR__.'/../../logs/hello.log',
                'level' => 'INFO'
            ]
        ],
        'loggers' => [
            'logger_name_here' => [
                'handlers' => ['console', 'file']
            ]
        ]
    ]

您可以在配置中添加任意数量的记录器,处理程序,格式化程序,处理器等。 Cascade将为您设置一切。

[免责声明]:我是Monolog-Cascade

的主要撰稿人