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

How to use PHP-DI PSR 11 and Repository #114

Open
izac1 opened this issue Sep 16, 2020 · 1 comment
Open

How to use PHP-DI PSR 11 and Repository #114

izac1 opened this issue Sep 16, 2020 · 1 comment

Comments

@izac1
Copy link

izac1 commented Sep 16, 2020

php di config

 $containerBuilder->addDefinitions([
        Atlas::class =>  function (ContainerInterface $c) {
            $settings = $c->get('settings')['atlas']['pdo'];
            $atlasBuilder = new AtlasBuilder(...$settings);
            $atlasBuilder->setFactory(function ($class) use ($c) {
                if ($c->has($class)) {
                    return $c->get($class);
                }
            });

            return $atlasBuilder->newAtlas();
        },
    ]);

Repository

class TestRepository
{
    protected $mapper;

    public function __construct(TestThread $mapper)
    {
        $this->mapper = $mapper;    
    }
}

Error
cannot be resolved: Entry "Atlas\Table\Table" cannot be resolved: the class is not instantiable

@sunkan
Copy link
Contributor

sunkan commented Sep 18, 2020

The problem is that you never add the mapper classes to the di definition.

I've a file that looks something like this:

<?php

use Atlas\Orm\Atlas;
use Psr\Container\ContainerInterface;

$mappers = [
	TestThread::class,
];

$defs = [];

foreach ($mappers as $mapper) {
	$defs[$mapper] = DI\factory(static function (ContainerInterface $container, string $mapper) {
		return $container->get(Atlas::class)->mapper($mapper);
	})->parameter('mapper', $mapper);
}

return $defs;

By doing it this way we don't initialize any mappers until we needed them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants