Skip to content

Commit

Permalink
Merge pull request #6 from peckadesign/pdp-3114-nette3
Browse files Browse the repository at this point in the history
#3114 Dopredne upravy pro nette3
  • Loading branch information
Jakub-Fajkus authored Dec 11, 2020
2 parents ead6e5e + d8611b5 commit 8d1236e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
],
"require": {
"php": "~7.1",
"nette/application": "~2.4.14",
"nette/di": "~2.4.14",
"kdyby/console": "~2.7.1",
"nette/application": "~2.4.14|~3.0.0",
"nette/di": "~2.4.14|~3.0.0",
"kdyby/console": "~2.7.1|dev-master",
"supervisorphp/configuration": "~0.2.1"
},
"require-dev": {
Expand Down
23 changes: 23 additions & 0 deletions src/Adapter/Nette/DI/CompilerExtensionAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace Pd\Supervisor\Adapter\Nette\DI;

class CompilerExtensionAdapter
{

/**
* Nahrazuje volani metody $this->getConfig() s parametrem, ktery obsahuje vychozi hodnoty.
* Zde je pouzita predchozi funkcionalita, nez byla z getConfig() odebrana.
* V Nette3 se pro validaci configu pouziva Schema. To by jsme pak meli pouzit namisto teto funkce.
*
* @param array<mixed> $defaults
* @return array<mixed>|string
*/
public static function mergeConfigWithDefaults(\Nette\DI\CompilerExtension $extension, array $defaults)
{
$defaultConfig = \Nette\DI\Helpers::expand($defaults, $extension->getContainerBuilder()->parameters);

return \Nette\DI\Config\Helpers::merge($extension->getConfig(), $defaultConfig);
}

}
27 changes: 27 additions & 0 deletions src/Adapter/Nette/DI/DiStatementFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Pd\Supervisor\Adapter\Nette\DI;

class DiStatementFactory
{
public static function createDiStatement($entity, array $arguments = [])
{
if (\class_exists(\Nette\DI\Definitions\Statement::class)) {
return self::createDiStatementNette30($entity, $arguments);
} else {
return self::createDiStatementNette24($entity, $arguments);
}
}


private static function createDiStatementNette24($entity, array $arguments = [])
{
return new \Nette\DI\Statement($entity, $arguments);
}


private static function createDiStatementNette30($entity, array $arguments = [])
{
return new \Nette\DI\Definitions\Statement($entity, $arguments);
}
}
20 changes: 11 additions & 9 deletions src/DI/SupervisorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Kdyby\Console\DI\ConsoleExtension;
use Nette\DI\CompilerExtension;
use Nette\DI\Helpers;
use Nette\DI\Statement;
use Pd\Supervisor\Console\RenderCommand;
use Pd\Supervisor\Console\WriteCommand;
use Supervisor\Configuration\Configuration;
Expand All @@ -28,7 +27,7 @@ public function loadConfiguration()
{
$builder = $this->getContainerBuilder();

$config = $this->getConfig(self::DEFAULTS);
$config = \Pd\Supervisor\Adapter\Nette\DI\CompilerExtensionAdapter::mergeConfigWithDefaults($this, self::DEFAULTS);

if ( ! isset($config['prefix'])) {
throw new \Pd\Supervisor\DI\MissingConfigurationValueException(
Expand All @@ -50,11 +49,11 @@ public function loadConfiguration()
);

$builder->addDefinition($this->prefix('renderCommand'))
->setClass(RenderCommand::class, [strtr($this->prefix('render'), '.', ':')])
->setFactory(RenderCommand::class, [strtr($this->prefix('render'), '.', ':')])
->addTag(ConsoleExtension::TAG_COMMAND)
;
$builder->addDefinition($this->prefix('writeCommand'))
->setClass(WriteCommand::class, [strtr($this->prefix('write'), '.', ':')])
->setFactory(WriteCommand::class, [strtr($this->prefix('write'), '.', ':')])
->addTag(ConsoleExtension::TAG_COMMAND)
;
}
Expand All @@ -65,7 +64,7 @@ private function loadSupervisorConfiguration(array $config, array $defaults = []
$builder = $this->getContainerBuilder();

$configuration = $builder->addDefinition($this->prefix('configuration'))
->setClass(Configuration::class)
->setType(Configuration::class)
;

foreach ($config as $sectionName => $sectionConfig) {
Expand All @@ -76,15 +75,15 @@ private function loadSupervisorConfiguration(array $config, array $defaults = []
foreach ((array) $sectionConfig as $name => $properties) {
$name = $this->prepareName($name, $prefix);
$configuration->addSetup('addSection', [
new Statement($sectionClass, [
\Pd\Supervisor\Adapter\Nette\DI\DiStatementFactory::createDiStatement($sectionClass, [
$name,
isset($defaults[$sectionName]) ? $this->mergeProperties($properties, $defaults[$sectionName]) : $properties,
]),
]);
}
} else {
$configuration->addSetup('addSection', [
new Statement(
\Pd\Supervisor\Adapter\Nette\DI\DiStatementFactory::createDiStatement(
$sectionClass, [
isset($defaults[$sectionName]) ? $this->mergeProperties($sectionConfig, $defaults[$sectionName]) : $sectionConfig,
]),
Expand Down Expand Up @@ -121,7 +120,10 @@ private function prepareName(string $name, string $prefix): string
}


private function prepareGroup(array $config, \Nette\DI\ServiceDefinition $configuration, string $prefix, string $group = NULL): void
/**
* @param \Nette\DI\ServiceDefinition|\Nette\DI\Definitions\ServiceDefinition $configuration
*/
private function prepareGroup(array $config, $configuration, string $prefix, string $group = NULL): void
{
if ( ! $group) {
return;
Expand All @@ -133,7 +135,7 @@ private function prepareGroup(array $config, \Nette\DI\ServiceDefinition $config

$sectionClass = (new Configuration)->findSection('group');
$configuration->addSetup('addSection', [
new Statement(
\Pd\Supervisor\Adapter\Nette\DI\DiStatementFactory::createDiStatement(
$sectionClass,
[
$group,
Expand Down

0 comments on commit 8d1236e

Please sign in to comment.