Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/Application/Bootloader/ComposerClientBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Butschster\ContextGenerator\Application\Bootloader;

use Butschster\ContextGenerator\Application\Logger\HasPrefixLoggerInterface;
use Butschster\ContextGenerator\Lib\ComposerClient\ComposerClientInterface;
use Butschster\ContextGenerator\Lib\ComposerClient\FileSystemComposerClient;
use Butschster\ContextGenerator\Source\Composer\Provider\ComposerProviderInterface;
use Butschster\ContextGenerator\Source\Composer\Provider\CompositeComposerProvider;
use Butschster\ContextGenerator\Source\Composer\Provider\LocalComposerProvider;
use Spiral\Boot\Bootloader\Bootloader;

final class ComposerClientBootloader extends Bootloader
Expand All @@ -14,6 +18,14 @@ final class ComposerClientBootloader extends Bootloader
public function defineSingletons(): array
{
return [
ComposerProviderInterface::class => static fn(
HasPrefixLoggerInterface $logger,
LocalComposerProvider $localProvider,
) => new CompositeComposerProvider(
logger: $logger->withPrefix('composer-provider'),
localProvider: $localProvider,
),

ComposerClientInterface::class => FileSystemComposerClient::class,
];
}
Expand Down
127 changes: 32 additions & 95 deletions src/Application/Bootloader/ConfigLoaderBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Butschster\ContextGenerator\Application\Bootloader;

use Butschster\ContextGenerator\Application\Logger\HasPrefixLoggerInterface;
use Butschster\ContextGenerator\Config\ConfigurationProvider;
use Butschster\ContextGenerator\Config\Import\ImportParserPlugin;
use Butschster\ContextGenerator\Config\Loader\ConfigLoaderFactory;
use Butschster\ContextGenerator\Config\Loader\ConfigLoaderFactoryInterface;
Expand All @@ -19,17 +17,13 @@
use Butschster\ContextGenerator\DirectoriesInterface;
use Butschster\ContextGenerator\Document\Compiler\DocumentCompiler;
use Butschster\ContextGenerator\Document\DocumentsParserPlugin;
use Butschster\ContextGenerator\Lib\Content\ContentBuilderFactory;
use Butschster\ContextGenerator\Lib\Variable\VariableResolver;
use Butschster\ContextGenerator\Modifier\Alias\AliasesRegistry;
use Butschster\ContextGenerator\Modifier\Alias\ModifierAliasesParserPlugin;
use Butschster\ContextGenerator\Modifier\Alias\ModifierResolver;
use Butschster\ContextGenerator\Modifier\SourceModifierRegistry;
use Butschster\ContextGenerator\Source\Registry\SourceProviderInterface;
use Butschster\ContextGenerator\SourceParserInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Config\Proxy;
use Spiral\Core\FactoryInterface;
use Spiral\Files\FilesInterface;

/**
Expand Down Expand Up @@ -61,99 +55,42 @@ public function registerParserPlugin(ConfigParserPluginInterface $plugin): void
public function defineSingletons(): array
{
return [
ParserPluginRegistry::class => function (
SourceProviderInterface $sourceProvider,
ImportParserPlugin $importParserPlugin,
HasPrefixLoggerInterface $logger,
) {
$modifierResolver = new ModifierResolver(
aliasesRegistry: $aliases = new AliasesRegistry(),
);

return new ParserPluginRegistry([
new ModifierAliasesParserPlugin(
aliasesRegistry: $aliases,
),
new DocumentsParserPlugin(
sources: $sourceProvider,
modifierResolver: $modifierResolver,
logger: $logger->withPrefix('documents-parser-plugin'),
),
$importParserPlugin, // Injected from ImportBootloader
...$this->parserPlugins,
]);
},

ConfigurationProvider::class => static fn(
ConfigLoaderFactoryInterface $configLoaderFactory,
DirectoriesInterface $dirs,
HasPrefixLoggerInterface $logger,
) => new ConfigurationProvider(
loaderFactory: $configLoaderFactory,
dirs: $dirs,
logger: $logger->withPrefix('config-provider'),
),
AliasesRegistry::class => AliasesRegistry::class,
ModifierResolver::class => ModifierResolver::class,
ParserPluginRegistry::class => fn(ImportParserPlugin $importParserPlugin, DocumentsParserPlugin $documentsParserPlugin, ModifierAliasesParserPlugin $modifierAliasesParserPlugin) => new ParserPluginRegistry([
$modifierAliasesParserPlugin,
$documentsParserPlugin,
$importParserPlugin,
...$this->parserPlugins,
]),

// ConfigurationProvider::class => static fn(
// ConfigLoaderFactoryInterface $configLoaderFactory,
// DirectoriesInterface $dirs,
// HasPrefixLoggerInterface $logger,
// ) => new ConfigurationProvider(
// loaderFactory: $configLoaderFactory,
// dirs: $dirs,
// logger: $logger->withPrefix('config-provider'),
// ),

DocumentCompiler::class => static fn(
FilesInterface $files,
SourceParserInterface $parser,
FactoryInterface $factory,
DirectoriesInterface $dirs,
SourceModifierRegistry $registry,
ContentBuilderFactory $builderFactory,
HasPrefixLoggerInterface $logger,
VariableResolver $variables,
) => new DocumentCompiler(
files: $files,
parser: $parser,
basePath: (string) $dirs->getOutputPath(),
modifierRegistry: $registry,
variables: $variables,
builderFactory: $builderFactory,
logger: $logger->withPrefix('document-compiler'),
) => $factory->make(DocumentCompiler::class, [
'basePath' => (string) $dirs->getOutputPath(),
]),

ConfigReaderRegistry::class => static fn(FilesInterface $files, JsonReader $jsonReader, YamlReader $yamlReader, PhpReader $phpReader) => new ConfigReaderRegistry(
readers: [
'json' => $jsonReader,
'yaml' => $yamlReader,
'yml' => $yamlReader,
'php' => $phpReader,
],
),

ConfigReaderRegistry::class => static function (
FilesInterface $files,
HasPrefixLoggerInterface $logger,
) {
// Create readers
$jsonReader = new JsonReader(
files: $files,
logger: $logger->withPrefix('json-reader'),
);

$yamlReader = new YamlReader(
files: $files,
logger: $logger->withPrefix('yaml-reader'),
);

$phpReader = new PhpReader(
files: $files,
logger: $logger->withPrefix('php-reader'),
);

return new ConfigReaderRegistry(
readers: [
'json' => $jsonReader,
'yaml' => $yamlReader,
'yml' => $yamlReader,
'php' => $phpReader,
],
);
},

ConfigLoaderFactoryInterface::class => static fn(
ConfigReaderRegistry $readers,
ParserPluginRegistry $pluginRegistry,
FilesInterface $files,
DirectoriesInterface $dirs,
HasPrefixLoggerInterface $logger,
) => new ConfigLoaderFactory(
readers: $readers,
pluginRegistry: $pluginRegistry,
dirs: $dirs,
logger: $logger->withPrefix('config-loader'),
),
ConfigLoaderFactoryInterface::class => ConfigLoaderFactory::class,

ConfigLoaderInterface::class => new Proxy(
interface: ConfigLoaderInterface::class,
Expand Down
5 changes: 4 additions & 1 deletion src/Application/Bootloader/HttpClientBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ final class HttpClientBootloader extends Bootloader
public function defineSingletons(): array
{
return [
HttpClientInterface::class => static fn(Client $httpClient, HttpFactory $httpMessageFactory) => HttpClientFactory::create(
HttpClientInterface::class => static fn(
Client $httpClient,
HttpFactory $httpMessageFactory,
) => HttpClientFactory::create(
$httpClient,
$httpMessageFactory,
),
Expand Down
65 changes: 7 additions & 58 deletions src/Application/Bootloader/ImportBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
use Butschster\ContextGenerator\Config\Import\Source\Local\LocalImportSource;
use Butschster\ContextGenerator\Config\Import\Source\Registry\ImportSourceRegistry;
use Butschster\ContextGenerator\Config\Import\Source\Url\UrlImportSource;
use Butschster\ContextGenerator\Config\Reader\ConfigReaderRegistry;
use Butschster\ContextGenerator\DirectoriesInterface;
use Butschster\ContextGenerator\Lib\HttpClient\HttpClientInterface;
use Butschster\ContextGenerator\Lib\Variable\VariableResolver;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Core\Attribute\Singleton;
use Spiral\Files\FilesInterface;

/**
* Bootloader for Import-related components
Expand All @@ -35,69 +30,23 @@ public function defineSingletons(): array
return [
// Import source registry with all sources registered
ImportSourceRegistry::class => static function (
FilesInterface $files,
ConfigReaderRegistry $readers,
HttpClientInterface $httpClient,
VariableResolver $variables,
HasPrefixLoggerInterface $logger,
LocalImportSource $localImportSource,
UrlImportSource $urlImportSource,
) {
$registry = new ImportSourceRegistry(
logger: $logger->withPrefix('import-source-registry'),
);

// Register all import sources

// Local import source (default)
$registry->register(
new LocalImportSource(
files: $files,
readers: $readers,
logger: $logger->withPrefix('import-source-local'),
),
);

// URL import source
$registry->register(
new UrlImportSource(
httpClient: $httpClient,
variables: $variables,
logger: $logger->withPrefix('import-source-url'),
),
);
$registry->register($localImportSource);
$registry->register($urlImportSource);

return $registry;
},

// Import source provider
ImportSourceProvider::class => static fn(
ImportSourceRegistry $sourceRegistry,
HasPrefixLoggerInterface $logger,
) => new ImportSourceProvider(
sourceRegistry: $sourceRegistry,
logger: $logger->withPrefix('import-sources'),
),

// Import resolver
ImportResolver::class => static fn(
FilesInterface $files,
DirectoriesInterface $dirs,
ImportSourceProvider $sourceProvider,
HasPrefixLoggerInterface $logger,
) => new ImportResolver(
dirs: $dirs,
files: $files,
sourceProvider: $sourceProvider,
logger: $logger->withPrefix('import-resolver'),
),

// Import parser plugin
ImportParserPlugin::class => static fn(
ImportResolver $importResolver,
HasPrefixLoggerInterface $logger,
) => new ImportParserPlugin(
importResolver: $importResolver,
logger: $logger->withPrefix('import-parser'),
),
ImportSourceProvider::class => ImportSourceProvider::class,
ImportResolver::class => ImportResolver::class,
ImportParserPlugin::class => ImportParserPlugin::class,
];
}

Expand Down
54 changes: 48 additions & 6 deletions src/Application/Bootloader/LoggerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,67 @@
namespace Butschster\ContextGenerator\Application\Bootloader;

use Butschster\ContextGenerator\Application\Logger\HasPrefixLoggerInterface;
use Butschster\ContextGenerator\Application\Logger\LoggerPrefix;
use Butschster\ContextGenerator\Application\Logger\NullLogger;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Core\BinderInterface;
use Spiral\Core\Config\Proxy;
use Spiral\Core\Container\InjectorInterface;

final class LoggerBootloader extends Bootloader
/**
* @implements InjectorInterface<LoggerInterface>
*/
final class LoggerBootloader extends Bootloader implements InjectorInterface
{
public function __construct(
private readonly ContainerInterface $container,
) {}

#[\Override]
public function defineSingletons(): array
{
return [
LoggerInterface::class => new Proxy(
interface: LoggerInterface::class,
fallbackFactory: static fn(): LoggerInterface => new NullLogger(),
),
HasPrefixLoggerInterface::class => new Proxy(
interface: HasPrefixLoggerInterface::class,
fallbackFactory: static fn(): LoggerInterface => new NullLogger(),
fallbackFactory: static fn(): HasPrefixLoggerInterface => new NullLogger(),
),
];
}

public function boot(BinderInterface $binder): void
{
// Register injectable class
$binder->bindInjector(LoggerInterface::class, self::class);
}

public function createInjection(\ReflectionClass $class, mixed $context = null): LoggerInterface
{
$logger = $this->container->get(HasPrefixLoggerInterface::class);

$prefix = null;
if ($context instanceof \ReflectionParameter) {
$prefix = $this->findAttribute($context)?->prefix ?? $context->getDeclaringClass()->getShortName();
}

if (!$prefix) {
return $logger;
}

return $logger->withPrefix($prefix);
}

private function findAttribute(\ReflectionParameter $parameter): ?LoggerPrefix
{
foreach ($parameter->getAttributes(LoggerPrefix::class) as $attribute) {
return $attribute->newInstance();
}

foreach ($parameter->getDeclaringClass()->getAttributes(LoggerPrefix::class) as $attribute) {
return $attribute->newInstance();
}

return null;
}
}
Loading
Loading