-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Install [shipmonk/composer-dependency-analyser](https://github.com/shipmonk-rnd/composer-dependency-analyser) 2. Add missing `require`s 3. Add configuration 4. Also check `services.neon` and `extension.neon` and add the classes to the config
- Loading branch information
Showing
40 changed files
with
3,487 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\DependencyInjection; | ||
|
||
use MichalSpacekCz\DependencyInjection\Exceptions\DiServicesConfigInvalidException; | ||
use Nette\Neon\Entity; | ||
use Nette\Neon\Neon; | ||
|
||
class DiServices | ||
{ | ||
|
||
/** @var array<non-empty-string, non-empty-lowercase-string> */ | ||
private array $configFiles = [ | ||
__DIR__ . '/../../config/services.neon' => 'services', | ||
__DIR__ . '/../../config/extensions.neon' => 'extensions', | ||
]; | ||
|
||
|
||
/** | ||
* @return list<class-string> | ||
*/ | ||
public function getAllClasses(): array | ||
{ | ||
$allServices = []; | ||
foreach ($this->configFiles as $file => $section) { | ||
$decoded = Neon::decodeFile($file); | ||
if (!is_array($decoded)) { | ||
throw new DiServicesConfigInvalidException($file, null, 'not an array'); | ||
} | ||
if (!isset($decoded[$section])) { | ||
throw new DiServicesConfigInvalidException($file, $section, "section doesn't exist"); | ||
} | ||
$services = $decoded[$section]; | ||
if (!is_array($services)) { | ||
throw new DiServicesConfigInvalidException($file, $section, "section not iterable"); | ||
} | ||
$services = array_filter($services, function (mixed $value): bool { | ||
return is_string($value) || is_array($value) || $value instanceof Entity; | ||
}); | ||
foreach ($services as $service) { | ||
$classString = $this->getString($service, $file, $section); | ||
if (str_starts_with($classString, '@')) { | ||
continue; | ||
} | ||
if (class_exists($classString) || interface_exists($classString)) { | ||
$allServices[] = $classString; | ||
} else { | ||
throw new DiServicesConfigInvalidException($file, $section, "class or interface '{$classString}' doesn't exist"); | ||
} | ||
} | ||
} | ||
return $allServices; | ||
} | ||
|
||
|
||
/** | ||
* @param string|Entity|array<array-key, mixed> $item | ||
*/ | ||
private function getString(string|Entity|array $item, string $file, string $section): string | ||
{ | ||
if (is_string($item)) { | ||
return $item; | ||
} elseif (is_array($item)) { | ||
if (isset($item['create'])) { | ||
if (is_string($item['create'])) { | ||
return $item['create']; | ||
} elseif ($item['create'] instanceof Entity && is_string($item['create']->value)) { | ||
return $item['create']->value; | ||
} | ||
} elseif (isset($item['type']) && is_string($item['type'])) { | ||
return $item['type']; | ||
} | ||
} elseif (is_string($item->value)) { | ||
return $item->value; | ||
} | ||
$message = is_array($item) ? sprintf("Unsupported array '%s'", json_encode($item)) : sprintf("Unsupported item '%s'", get_debug_type($item)); | ||
throw new DiServicesConfigInvalidException($file, $section, $message); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
site/app/DependencyInjection/Exceptions/DiServicesConfigInvalidException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\DependencyInjection\Exceptions; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
class DiServicesConfigInvalidException extends Exception | ||
{ | ||
|
||
public function __construct(?string $file, ?string $section, string $message, ?Throwable $previous = null) | ||
{ | ||
$ident = $file !== null ? "{$file}:" : ''; | ||
if ($section !== null) { | ||
$ident .= "{$section}:"; | ||
} | ||
parent::__construct("{$ident} {$message}", previous: $previous); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
use MichalSpacekCz\Application\Bootstrap; | ||
use MichalSpacekCz\Application\Cli\NoCliArgs; | ||
use MichalSpacekCz\DependencyInjection\DiServices; | ||
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration; | ||
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType; | ||
|
||
$allClasses = Bootstrap::bootCli(NoCliArgs::class)->getByType(DiServices::class)->getAllClasses(); | ||
|
||
return (new Configuration()) | ||
// Add classes from services.neon and extensions.neon | ||
->addForceUsedSymbols($allClasses) | ||
|
||
// Attributes used for development only | ||
->ignoreErrorsOnPackage('jetbrains/phpstorm-attributes', [ErrorType::DEV_DEPENDENCY_IN_PROD]) | ||
|
||
// It's used, believe me | ||
->ignoreErrorsOnPackage('latte/latte', [ErrorType::UNUSED_DEPENDENCY]) | ||
|
||
// TestCaseRunner is used only in tests | ||
->ignoreErrorsOnPackageAndPath('nette/tester', __DIR__ . '/app/Test/TestCaseRunner.php', [ErrorType::DEV_DEPENDENCY_IN_PROD]); | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.