Skip to content

Commit

Permalink
Merge pull request #319 from gacela-project/feat/use-provider
Browse files Browse the repository at this point in the history
Add AbstractProvider
  • Loading branch information
Chemaclass authored Aug 17, 2024
2 parents 53ac6e2 + 047d783 commit e37cca8
Show file tree
Hide file tree
Showing 74 changed files with 332 additions and 334 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed disable event listeners
- Add `Gacela::addGlobal()`
- Add `Gacela::overrideExistingResolvedClass()`
- Deprecate `AbstractDependencyProvider` in favor of `AbstractProvider`

## [1.7.1](https://github.com/gacela-project/gacela/compare/1.7.0...1.7.1) - 2024-04-16

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ It encourages your modules to interact with each other in a unified way by follo
- Modules interact with each other **only** via their **Facade**
- The [**Facade**](https://gacela-project.com/docs/facade/) is the *entry point* of a module
- The [**Factory**](https://gacela-project.com/docs/factory/) manage the *intra-dependencies* the module
- The [**DependencyProvider**](https://gacela-project.com/docs/dependency-provider/) resolves the *extra-dependencies* of the module
- The [**Config**](https://gacela-project.com/docs/config/) has access to the project's *config files*
- The [**Provider**](https://gacela-project.com/docs/provider/) resolves the *extra-dependencies* of the module
- The [**Config**](https://gacela-project.com/docs/config/) access the project's *config files*

### Installation

Expand Down Expand Up @@ -70,7 +70,7 @@ application-name
│ │ │ # These are the 4 "gacela classes":
│ │ ├── Facade.php
│ │ ├── Factory.php
│ │ ├── DependencyProvider.php
│ │ ├── Provider.php
│ │ └── Config.php
│ │
│ └── ModuleB
Expand Down
4 changes: 2 additions & 2 deletions src/Console/ConsoleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function getConfigMakerTemplate(): string
return $this->getCommandTemplateContent('config-maker.txt');
}

public function getDependencyProviderMakerTemplate(): string
public function getProviderMakerTemplate(): string
{
return $this->getCommandTemplateContent('dependency-provider-maker.txt');
return $this->getCommandTemplateContent('provider-maker.txt');
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Console/ConsoleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Gacela\Console\Infrastructure\FileContentIo;
use Gacela\Framework\AbstractFactory;
use Gacela\Framework\ClassResolver\Config\ConfigResolver;
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderResolver;
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
use Gacela\Framework\ClassResolver\Provider\ProviderResolver;
use Gacela\Framework\Gacela;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
Expand All @@ -36,7 +36,7 @@ final class ConsoleFactory extends AbstractFactory
*/
public function getConsoleCommands(): array
{
return (array)$this->getProvidedDependency(ConsoleDependencyProvider::COMMANDS);
return (array)$this->getProvidedDependency(ConsoleProvider::COMMANDS);

Check warning on line 39 in src/Console/ConsoleFactory.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "CastArray": --- Original +++ New @@ @@ */ public function getConsoleCommands() : array { - return (array) $this->getProvidedDependency(ConsoleProvider::COMMANDS); + return $this->getProvidedDependency(ConsoleProvider::COMMANDS); } public function createCommandArgumentsParser() : CommandArgumentsParserInterface {
}

public function createCommandArgumentsParser(): CommandArgumentsParserInterface
Expand Down Expand Up @@ -72,7 +72,7 @@ public function createAppModuleCreator(): AppModuleCreator
return new AppModuleCreator(
new FactoryResolver(),
new ConfigResolver(),
new DependencyProviderResolver(),
new ProviderResolver(),
);
}

Expand All @@ -99,6 +99,6 @@ private function createFileContentIo(): FileContentIoInterface
*/
private function getTemplateByFilenameMap(): array
{
return (array)$this->getProvidedDependency(ConsoleDependencyProvider::TEMPLATE_BY_FILENAME_MAP);
return (array)$this->getProvidedDependency(ConsoleProvider::TEMPLATE_BY_FILENAME_MAP);

Check warning on line 102 in src/Console/ConsoleFactory.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "CastArray": --- Original +++ New @@ @@ */ private function getTemplateByFilenameMap() : array { - return (array) $this->getProvidedDependency(ConsoleProvider::TEMPLATE_BY_FILENAME_MAP); + return $this->getProvidedDependency(ConsoleProvider::TEMPLATE_BY_FILENAME_MAP); } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use Gacela\Console\Infrastructure\Command\ListModulesCommand;
use Gacela\Console\Infrastructure\Command\MakeFileCommand;
use Gacela\Console\Infrastructure\Command\MakeModuleCommand;
use Gacela\Framework\AbstractDependencyProvider;
use Gacela\Framework\AbstractProvider;
use Gacela\Framework\Container\Container;

/**
* @method ConsoleConfig getConfig()
*/
final class ConsoleDependencyProvider extends AbstractDependencyProvider
final class ConsoleProvider extends AbstractProvider
{
public const COMMANDS = 'COMMANDS';

Expand All @@ -41,7 +41,7 @@ private function addTemplateByFilenameMap(Container $container): void
FilenameSanitizer::FACADE => $this->getConfig()->getFacadeMakerTemplate(),
FilenameSanitizer::FACTORY => $this->getConfig()->getFactoryMakerTemplate(),
FilenameSanitizer::CONFIG => $this->getConfig()->getConfigMakerTemplate(),
FilenameSanitizer::DEPENDENCY_PROVIDER => $this->getConfig()->getDependencyProviderMakerTemplate(),
FilenameSanitizer::PROVIDER => $this->getConfig()->getProviderMakerTemplate(),
]);
}
}
6 changes: 3 additions & 3 deletions src/Console/Domain/AllAppModules/AppModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(
private readonly string $facadeClass,
private readonly ?string $factoryClass = null,
private readonly ?string $configClass = null,
private readonly ?string $dependencyProviderClass = null,
private readonly ?string $providerClass = null,
) {
}

Expand Down Expand Up @@ -53,8 +53,8 @@ public function configClass(): ?string
/**
* @return ?class-string
*/
public function dependencyProviderClass(): ?string
public function providerClass(): ?string
{
return $this->dependencyProviderClass;
return $this->providerClass;
}
}
16 changes: 8 additions & 8 deletions src/Console/Domain/AllAppModules/AppModuleCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Gacela\Console\Domain\AllAppModules;

use Gacela\Framework\ClassResolver\Config\ConfigResolver;
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderNotFoundException;
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderResolver;
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
use Gacela\Framework\ClassResolver\Provider\ProviderNotFoundException;
use Gacela\Framework\ClassResolver\Provider\ProviderResolver;
use ReflectionClass;

use function strlen;
Expand All @@ -17,7 +17,7 @@ final class AppModuleCreator
public function __construct(
private readonly FactoryResolver $factoryResolver,
private readonly ConfigResolver $configResolver,
private readonly DependencyProviderResolver $dependencyProviderResolver,
private readonly ProviderResolver $providerResolver,
) {
}

Expand All @@ -32,7 +32,7 @@ public function fromClass(string $facadeClass): AppModule
$facadeClass,
$this->findFactory($facadeClass),
$this->findConfig($facadeClass),
$this->findDependencyProvider($facadeClass),
$this->findProvider($facadeClass),
);
}

Expand Down Expand Up @@ -89,17 +89,17 @@ private function findConfig(string $facadeClass): ?string
/**
* @param class-string $facadeClass
*/
private function findDependencyProvider(string $facadeClass): ?string
private function findProvider(string $facadeClass): ?string
{
try {
$resolver = $this->dependencyProviderResolver->resolve($facadeClass);
$resolver = $this->providerResolver->resolve($facadeClass);

if ((new ReflectionClass($resolver))->isAnonymous()) {
throw new DependencyProviderNotFoundException($resolver);
throw new ProviderNotFoundException($resolver);
}

return $resolver::class;
} catch (DependencyProviderNotFoundException) {
} catch (ProviderNotFoundException) {
return null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Domain/FilenameSanitizer/FilenameSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ final class FilenameSanitizer implements FilenameSanitizerInterface

public const CONFIG = 'Config';

public const DEPENDENCY_PROVIDER = 'DependencyProvider';
public const PROVIDER = 'Provider';

public const EXPECTED_FILENAMES = [
self::FACADE,
self::FACTORY,
self::CONFIG,
self::DEPENDENCY_PROVIDER,
self::PROVIDER,
];

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Infrastructure/Command/ListModulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private function generateDetailedView(array $modules): void
$n = $i + 1;
$factory = $module->factoryClass() ?? self::CROSS_SYMBOL;
$config = $module->configClass() ?? self::CROSS_SYMBOL;
$dependencyProviderClass = $module->dependencyProviderClass() ?? self::CROSS_SYMBOL;
$provider = $module->providerClass() ?? self::CROSS_SYMBOL;

$result .= <<<TXT
============================
Expand All @@ -86,7 +86,7 @@ private function generateDetailedView(array $modules): void
<fg=cyan>Facade</>: {$module->facadeClass()}
<fg=cyan>Factory</>: {$factory}
<fg=cyan>Config</>: {$config}
<fg=cyan>DependencyProvider</>: {$dependencyProviderClass}
<fg=cyan>Provider</>: {$provider}
TXT;
}
Expand All @@ -107,7 +107,7 @@ private function generateSimpleView(array $modules): void
self::CHECK_SYMBOL, // facade is always true
$module->factoryClass() !== null ? self::CHECK_SYMBOL : self::CROSS_SYMBOL,
$module->configClass() !== null ? self::CHECK_SYMBOL : self::CROSS_SYMBOL,
$module->dependencyProviderClass() !== null ? self::CHECK_SYMBOL : self::CROSS_SYMBOL,
$module->providerClass() !== null ? self::CHECK_SYMBOL : self::CROSS_SYMBOL,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ declare(strict_types=1);

namespace $NAMESPACE$;

use Gacela\Framework\AbstractDependencyProvider;
use Gacela\Framework\AbstractProvider;
use Gacela\Framework\Container\Container;

final class $CLASS_NAME$ extends AbstractDependencyProvider
final class $CLASS_NAME$ extends AbstractProvider
{
public function provideModuleDependencies(Container $container): void
{
Expand Down
10 changes: 4 additions & 6 deletions src/Framework/AbstractDependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Gacela\Framework;

use Gacela\Framework\Container\Container;

abstract class AbstractDependencyProvider
/**
* @deprecated in favor of AbstractProvider
*/
abstract class AbstractDependencyProvider extends AbstractProvider
{
use ConfigResolverAwareTrait;

abstract public function provideModuleDependencies(Container $container): void;
}
49 changes: 48 additions & 1 deletion src/Framework/AbstractFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,54 @@

namespace Gacela\Framework;

use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
use RuntimeException;

/**
* The `__callStatic` and `__call` methods allow defining `getFactory` as static and non-static methods.
*
* @psalm-suppress MethodSignatureMismatch
*
* @method static AbstractFactory getFactory()
* @method AbstractFactory getFactory()
*/
abstract class AbstractFacade
{
use FactoryResolverAwareTrait;
/** @var array<string, AbstractFactory> */
private static array $factories = [];

/**
* @param list<mixed> $arguments
*/
public static function __callStatic(string $name = '', array $arguments = [])
{
if ($name === 'getFactory') {
return self::doGetFactory();
}

throw new RuntimeException(sprintf("Method unknown: '%s'", $name));
}

/**
* @param list<mixed> $arguments
*/
public function __call(string $name = '', array $arguments = [])
{
if ($name === 'getFactory') {
return self::doGetFactory();
}

throw new RuntimeException(sprintf("Method unknown: '%s'", $name));
}

public static function resetCache(): void
{
self::$factories = [];
}

private static function doGetFactory(): AbstractFactory
{
return self::$factories[static::class]
??= (new FactoryResolver())->resolve(static::class);
}
}
6 changes: 3 additions & 3 deletions src/Framework/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Gacela\Framework;

use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderResolver;
use Gacela\Framework\ClassResolver\Provider\ProviderResolver;
use Gacela\Framework\Config\Config;
use Gacela\Framework\Container\Container;

Expand Down Expand Up @@ -43,8 +43,8 @@ private function createContainerWithProvidedDependencies(): Container
{
$container = Container::withConfig(Config::getInstance());

$dependencyProvider = (new DependencyProviderResolver())->resolve($this);
$dependencyProvider->provideModuleDependencies($container);
$provider = (new ProviderResolver())->resolve($this);
$provider->provideModuleDependencies($container);

return $container;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Framework/AbstractProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Gacela\Framework;

use Gacela\Framework\Container\Container;

abstract class AbstractProvider
{
use ConfigResolverAwareTrait;

abstract public function provideModuleDependencies(Container $container): void;
}
4 changes: 2 additions & 2 deletions src/Framework/Bootstrap/GacelaConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public function addSuffixTypeConfig(string $suffix): self
/**
* Allow overriding gacela dependency provider suffixes.
*/
public function addSuffixTypeDependencyProvider(string $suffix): self
public function addSuffixTypeProvider(string $suffix): self
{
$this->suffixTypesBuilder->addDependencyProvider($suffix);
$this->suffixTypesBuilder->addProvider($suffix);

return $this;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class AnonymousGlobal
private const ALLOWED_TYPES_FOR_ANONYMOUS_GLOBAL = [
'Config',
'Factory',
'DependencyProvider',
'Provider',
];

/** @var array<string,object> */
Expand Down Expand Up @@ -62,7 +62,7 @@ public static function getByKey(string $key): ?object
}

/**
* Add an anonymous class as 'Config', 'Factory' or 'DependencyProvider' as a global resource
* Add an anonymous class as 'Config', 'Factory' or 'Provider' as a global resource
* bound to the context that it's pass as first argument. It can be the string-key
* (from a non-class/file context) or the class/object itself.
*/
Expand Down
Loading

0 comments on commit e37cca8

Please sign in to comment.