Skip to content

Commit

Permalink
Refactor to use contributte/di
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jun 28, 2019
1 parent 6b259ff commit 7790027
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
],
"require": {
"php": "^7.2.0",
"contributte/di": "^0.4.0",
"monolog/monolog": "^1.23.0",
"nette/di": "~3.0.0",
"nette/utils": "~3.0.0"
},
"require-dev": {
Expand Down
25 changes: 4 additions & 21 deletions src/DI/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\Monolog\DI;

use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Contributte\Monolog\LoggerHolder;
use Contributte\Monolog\LoggerManager;
Expand All @@ -14,7 +15,6 @@
use Nette\PhpGenerator\ClassType;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Strings;
use stdClass;
use Tracy\Bridges\Psr\PsrToTracyLoggerAdapter;
use Tracy\Bridges\Psr\TracyToPsrLoggerAdapter;
Expand Down Expand Up @@ -46,28 +46,11 @@ public function getConfigSchema(): Schema
]);
}

/**
* Used in loadConfiguration phase when definition of service defined in services cannot be get
*
* @param string|mixed[]|Statement $config
* @return Definition|string
*/
private function getDefinitionFromConfig($config, string $preferredPrefix)
{
if (is_string($config) && Strings::startsWith($config, '@')) {
return $config;
}

$prefix = $preferredPrefix;
$this->compiler->loadDefinitionsFromConfig([$prefix => $config]);

return $this->getContainerBuilder()->getDefinition($prefix);
}

public function loadConfiguration(): void
{
$config = $this->config;
$builder = $this->getContainerBuilder();
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);

if (!isset($config->channel['default'])) {
throw new InvalidStateException(sprintf('%s.channel.default is required.', $this->name));
Expand Down Expand Up @@ -99,7 +82,7 @@ public function loadConfiguration(): void

foreach ($channel->handlers as $handlerName => $handlerConfig) {
$handlerPrefix = $this->prefix('logger.' . $name . '.handler.' . $handlerName);
$handlerDefinition = $this->getDefinitionFromConfig($handlerConfig, $handlerPrefix);
$handlerDefinition = $definitionsHelper->getDefinitionFromConfig($handlerConfig, $handlerPrefix);

if ($handlerDefinition instanceof Definition) {
$handlerDefinition->setAutowired(false);
Expand All @@ -113,7 +96,7 @@ public function loadConfiguration(): void

foreach ($channel->processors as $processorName => $processorConfig) {
$processorPrefix = $this->prefix('logger.' . $name . '.processor.' . $processorName);
$processorDefinition = $this->getDefinitionFromConfig($processorConfig, $processorPrefix);
$processorDefinition = $definitionsHelper->getDefinitionFromConfig($processorConfig, $processorPrefix);

if ($processorDefinition instanceof Definition) {
$processorDefinition->setAutowired(false);
Expand Down

0 comments on commit 7790027

Please sign in to comment.