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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add a Dependency Inversion Container Class to Pancake #235

Open
3 tasks
guibranco opened this issue Oct 21, 2024 · 2 comments · May be fixed by #251
Open
3 tasks

[FEATURE] Add a Dependency Inversion Container Class to Pancake #235

guibranco opened this issue Oct 21, 2024 · 2 comments · May be fixed by #251
Labels
♻️ code quality Code quality-related tasks or issues 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🕔 high effort A task that can be completed in a few days 🧪 tests Tasks related to testing 🛠 WIP Work in progress

Comments

@guibranco
Copy link
Owner

guibranco commented Oct 21, 2024

Description:
I would like to add a Dependency Inversion Container class to the Pancake project. This class should be responsible for handling service (class) registrations and resolving these services when needed, following the Dependency Injection (DI) pattern. This will improve modularity and flexibility within the Pancake framework.

The class should support:

  • Service registration.
  • Resolution of services with dependency support.
  • Singleton and transient lifecycles for services.
  • PSR-11

Below is an example of the DIContainer.php file to illustrate the expected implementation:

<?php

namespace GuiBranco\Pancake;

use Psr\Container\ContainerInterface;
use ReflectionClass;

class DIContainer implements ContainerInterface
{
    private array $services = [];
    private array $sharedInstances = [];

    public function has(string $name): bool
    {
        return isset($this->services[$name]);
    }

    public function get(string $name)
    {
        if (!isset($this->services[$name])) {
            throw new \Exception("Service '{$name}' not registered.");
        }

        // TODO:  check for the shared flag here before returning a new instance (like the resolve method already does).

        if (is_callable($this->services[$name])) {
            return $this->services[$name]($this);
        }

        return $this->resolve($this->services[$name]);
    }

    public function register(string $name, callable $resolver, bool $shared = false)
    {
        $this->services[$name] = [
            'resolver' => $resolver,
            'shared' => $shared,
        ];
    }
    
    protected function resolve(string $name)
    {
        if (!isset($this->services[$name])) {
            throw new \Exception("Service '{$name}' not registered.");
        }

        if ($this->services[$name]['shared']) {
            if (!isset($this->sharedInstances[$name])) {
                $this->sharedInstances[$name] = $this->services[$name]['resolver']($this);
            }
            return $this->sharedInstances[$name];
        }

        return $this->services[$name]['resolver']($this);
    }

    public function registerSingleton(string $name, callable $resolver)
    {
        $this->register($name, $resolver, true);
    }

    public function registerTransient(string $name, callable $resolver)
    {
        $this->register($name, $resolver, false);
    }
}

Task Requirements:

  • Implement the Dependency Inversion Container class for service registration and resolution.
  • Ensure that services can be registered as singletons (shared) or transient instances.
  • Automatically resolve dependencies when required.
  • Provide optional constructor parameter support for services that require other dependencies.

Additional Requirements:

  • Provide unit tests to ensure:
    • Services are correctly registered and resolved.
    • Singleton and transient lifecycles work as expected.
    • Dependencies are correctly resolved when registered.
  • Include integration tests to validate the DI container's functionality within the broader Pancake framework.

Acceptance Criteria:

  • The DI container is implemented and supports the required features.
  • Unit and integration tests are provided to verify the container’s behavior.
  • Documentation is updated to include usage examples for registering and resolving services.
Copy link
Contributor

gitauto-ai bot commented Oct 21, 2024

Click the checkbox below to generate a PR!

  • Generate PR

@guibranco, You have 5 requests left in this cycle which refreshes on 2024-11-21 10:07:38+00:00.
If you have any questions or concerns, please contact us at [email protected].

@guibranco guibranco added 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event 🧪 tests Tasks related to testing ♻️ code quality Code quality-related tasks or issues 🕔 high effort A task that can be completed in a few days labels Oct 21, 2024
@gitauto-ai gitauto-ai bot added the gitauto GitAuto label to trigger the app in a issue. label Oct 24, 2024
Copy link
Contributor

gitauto-ai bot commented Oct 24, 2024

Hey, I'm a bit lost here! Not sure which file I should be fixing. Could you give me a bit more to go on? Maybe add some details to the issue or drop a comment with some extra hints? Thanks!

Have feedback or need help?
Feel free to email [email protected].

@gstraccini gstraccini bot added the 🛠 WIP Work in progress label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
♻️ code quality Code quality-related tasks or issues 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🕔 high effort A task that can be completed in a few days 🧪 tests Tasks related to testing 🛠 WIP Work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant