Submit
Path:
~
/
/
opt
/
psa
/
admin
/
plib
/
modules
/
grafana
/
vendor
/
elie29
/
zend-phpdi-config
/
src
/
Tool
/
File Content:
AutowiresConfigDumper.php
<?php declare (strict_types=1); namespace PleskGrafana\Elie\PHPDI\Config\Tool; use InvalidArgumentException; use function class_exists; use function date; use function implode; use function in_array; use function is_array; use function is_int; use function is_string; use function sprintf; use function str_repeat; use function var_export; class AutowiresConfigDumper { private const CONFIG_TEMPLATE = <<<EOC <?php /** * This file generated by %s. * Generated %s */ return %s; EOC; private const AUTOWIRES = 'autowires'; public function createDependencyConfig(array $config, string $className) : array { $config['dependencies'] ??= []; if (!is_array($config['dependencies'])) { throw new InvalidArgumentException('Configuration dependencies key must be an array'); } $config['dependencies'] = $this->addAutowires($config['dependencies'], $className); return $config; } public function dumpConfigFile(array $config) : string { $prepared = $this->prepareConfig($config); return sprintf(self::CONFIG_TEMPLATE, static::class, date('Y-m-d H:i:s'), $prepared); } private function addAutowires(array $dependencies, string $entry) : array { if (!isset($dependencies[self::AUTOWIRES]) || !is_array($dependencies[self::AUTOWIRES])) { $dependencies[self::AUTOWIRES] = []; } // Add the class name as an entry to the autowires configuration if (!in_array($entry, $dependencies[self::AUTOWIRES], \true)) { $dependencies[self::AUTOWIRES][] = $entry; } return $dependencies; } private function prepareConfig(array $config, int $indentLevel = 1) : string { $indent = str_repeat(' ', $indentLevel * 4); $entries = []; foreach ($config as $key => $value) { $key = $this->createConfigKey($key); $entries[] = sprintf('%s%s%s,', $indent, $key ? sprintf('%s => ', $key) : '', $this->createConfigValue($value, $indentLevel)); } $outerIndent = str_repeat(' ', ($indentLevel - 1) * 4); return sprintf("[\n%s\n%s]", implode("\n", $entries), $outerIndent); } private function createConfigKey(mixed $key) : ?string { if (is_string($key) && class_exists($key)) { return sprintf('\\%s::class', $key); } if (is_int($key)) { return null; } return sprintf("'%s'", $key); } private function createConfigValue(mixed $value, int $indentLevel) : string { if (is_array($value)) { return $this->prepareConfig($value, $indentLevel + 1); } if (is_string($value) && class_exists($value)) { return sprintf('\\%s::class', $value); } return var_export($value, \true); } }
Submit
FILE
FOLDER
INFO
Name
Size
Permission
Action
AutowiresConfigDumper.php
2854 bytes
0644
AutowiresConfigDumperCommand.php
5185 bytes
0644
N4ST4R_ID | Naxtarrr