Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add container extend method #231

Merged
merged 11 commits into from Nov 14, 2022
Merged

Add container extend method #231

merged 11 commits into from Nov 14, 2022

Conversation

Chemaclass
Copy link
Member

@Chemaclass Chemaclass commented Nov 13, 2022

馃摎 Description

Currently, there is no possibility of extending a service from the Container in any way. However, it would be handy to be able to extend a service functionality from your GacelaConfig configuration.

馃敄 Changes

  • Added GacelaConfig::extendService(id, service)

馃И Example

Note: tests/Feature/Framework/ExtendService/FeatureTest.php

Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void {
    $config->extendService( # <------------ this is the new method
        DependencyProvider::ARRAY_AS_OBJECT,
        static function (ArrayObject $arrayObject): void {
            $arrayObject->append(3);
        }
    );
});
/************************************************************************/
final class DependencyProvider extends AbstractDependencyProvider
{
    public const ARRAY_AS_OBJECT = 'ARRAY_AS_OBJECT';

    public function provideModuleDependencies(Container $container): void
    {
        $container->set(self::ARRAY_AS_OBJECT, new ArrayObject([1, 2]));
    }
}
/************************************************************************/
final class Factory extends AbstractFactory
{
    public function getArrayAsObject(): ArrayObject
    {
        return $this->getProvidedDependency(DependencyProvider::ARRAY_AS_OBJECT);
    }
}
/************************************************************************/
final class Facade extends AbstractFacade
{
    public function getArrayAsObject(): ArrayObject
    {
        return $this->getFactory()->getArrayAsObject();
    }
}

/************************************************************************/
$facade = new Module\Facade();
$facade->getArrayAsObject(); // === new ArrayObject([1, 2, 3])

@Chemaclass Chemaclass added the enhancement New feature or request label Nov 13, 2022
@Chemaclass Chemaclass self-assigned this Nov 13, 2022
Comment on lines +5 to +6
- Added `Container::factory(service)`
- Added `Container::extend(id, service)`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is just about extend(). The factory() was implemented here: #229

@Chemaclass Chemaclass marked this pull request as draft November 13, 2022 19:06
@Chemaclass Chemaclass marked this pull request as ready for review November 13, 2022 20:31
Copy link
Member

@JesusValera JesusValera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to write some doc regarding these changes 馃憖

@Chemaclass Chemaclass merged commit 556ae02 into master Nov 14, 2022
@Chemaclass Chemaclass deleted the feature/container-extend branch November 14, 2022 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

None yet

2 participants