Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,21 @@ supervisor:
program:
program-name:
command: moo

overrides:
- match:
name: '/program-name/i'
property: '/numprocs/i'
value: '/([1-9]\d+|[2-9])/'
value: 1
```

Overrides add new option, if you have multiple neons with a lot of commands, and you need on test/dev/docker environment use only one process instead of many, you can easily do so with set of rules, that will override setup by neon (mainly to not include definitions on multiple files).

It is based on RegEx matcher, that will check if name is correct, if property name is correct and if value is correct (all of these are optional, but at least one must be present). If RegEx pass all rules, it will change the value.

In example above, the match will get every app that name contains program-name, have property numprocs and that property is bigger then 1, then replace it to number 1

## Commands

### RenderCommand
Expand Down
63 changes: 55 additions & 8 deletions src/DI/SupervisorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function loadConfiguration(): void
$this->loadSupervisorConfiguration(
(array) $config['configuration'],
(array) $config['defaults'],
(array) $config['overrides'],
(string) $config['prefix'],
isset($config['group']) ? (string) $config['group'] : NULL
);
Expand All @@ -59,24 +60,44 @@ public function getConfigSchema(): \Nette\Schema\Schema
{
return \Nette\Schema\Expect::structure([
'prefix' => \Nette\Schema\Expect::string()->nullable(),
'configuration' => \Nette\Schema\Expect::array(),
'defaults' => \Nette\Schema\Expect::array(),
'configuration' => \Nette\Schema\Expect::array()->default([]),
'overrides' => $this->getOverrideSchema()->default([]),
'defaults' => \Nette\Schema\Expect::array()->default([]),
'group' => \Nette\Schema\Expect::string()->nullable(),
]);
}

public function getOverrideSchema(): \Nette\Schema\Elements\Type
{
$isRegExp = static function (string $value): bool {
try {
return @preg_match($value, '') !== FALSE; // @ - is intentional
} catch (\Throwable $e) {
return FALSE;
}
};

return \Nette\Schema\Expect::arrayOf(
\Nette\Schema\Expect::structure([
'match' => \Nette\Schema\Expect::structure([
'name' => \Nette\Schema\Expect::string()->required(false)->assert($isRegExp, 'Must be valid regular expression'),
'property' => \Nette\Schema\Expect::string()->required(false)->assert($isRegExp, 'Must be valid regular expression'),
'value' => \Nette\Schema\Expect::string()->required(false)->assert($isRegExp, 'Must be valid regular expression'),
])->castTo('array'),
'value' => \Nette\Schema\Expect::scalar(),
])->castTo('array'),
);
}


/**
* @param array<string, mixed> $config
* @param array<string, mixed> $defaults
*/
private function loadSupervisorConfiguration(array $config, array $defaults, string $prefix, ?string $group): void
private function loadSupervisorConfiguration(array $config, array $defaults, array $overrides, string $prefix, ?string $group): void
{
$builder = $this->getContainerBuilder();

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

foreach ($config as $sectionName => $sectionConfig) {
if ( ! $sectionClass = (new Configuration)->findSection($sectionName)) {
Expand All @@ -85,10 +106,13 @@ private function loadSupervisorConfiguration(array $config, array $defaults, str
if (is_subclass_of($sectionClass, Named::class)) {
foreach ((array) $sectionConfig as $name => $properties) {
$name = $this->prepareName($name, $prefix);
$properties = isset($defaults[$sectionName]) ? $this->mergeProperties($properties, $defaults[$sectionName]) : $properties;
$properties = $this->processOverrides($overrides, $name, $properties);

$configuration->addSetup('addSection', [
new \Nette\DI\Definitions\Statement($sectionClass, [
$name,
isset($defaults[$sectionName]) ? $this->mergeProperties($properties, $defaults[$sectionName]) : $properties,
$properties,
]),
]);
}
Expand All @@ -105,6 +129,29 @@ private function loadSupervisorConfiguration(array $config, array $defaults, str
$this->prepareGroup($config, $configuration, $prefix, $group);
}

private function processOverrides(array $overrides, string $name, array $properties): array
{
foreach ($overrides as $item) {
if (!\is_null($item['match']['name']) && !preg_match($item['match']['name'], $name)) {
continue;
}

foreach ($properties as $property => $value) {
if (!\is_null($item['match']['property']) && !preg_match($item['match']['property'], $property)) {
continue;
}

if (!\is_null($item['match']['value']) && !preg_match($item['match']['value'], (string) $value)) {
continue;
}

$properties[$property] = $item['value'];
}
}

return $properties;
}


/**
* @param array<string, mixed> $properties
Expand Down