简体   繁体   中英

PHP-DI using previously defined class

I am trying to improve my PHP-DI config, and now I stuck.

I want to reuse an already defined class for another class.

entityManager class should come from Database->getEntityManager

Here is a part of my di config file:

return [
    'database.user'     => 'xxxxxxx',
    'database.password' => 'xxxxxxx',
    'database.name'     => 'xxxxxxx',
    'database.host'     => '127.0.0.1',
    'database.port'     => 3306,

    \ABCData\Database\Database::class => DI\autowire()->constructor(
        DI\get('database.user'),
        DI\get('database.password'),
        DI\get('database.name'),
        DI\get('database.host'),
        DI\get('database.port')
    ),


//    \Doctrine\ORM\EntityManager::class => DI\factory(function () {
//        $database = new ABCData\Database\Database(
//            \ABCData\Database\Config::DB_USER,
//            \ABCData\Database\Config::DB_PASSWORD,
//            \ABCData\Database\Config::DB_DATABASE,
//            \ABCData\Database\Config::DB_HOST
//        );
//        return $database->getEntityManager();
//    }),


    \Doctrine\ORM\EntityManager::class => DI\autowire(\ABCData\Database\Database::class)->method('getEntityManager'),
...

When I am injecting a database, that is works fine. The commented definition is works well, but I want to achive is the last line but it fails.

Here is my full implementation of my database class:

private $userName;

private $password;

private $database;

private $host;

private $port;

public function __construct($username, $password, $database, $host, $port = 3306)
{
    $this->userName = $username;
    $this->password = $password;
    $this->database = $database;
    $this->host = $host;
    $this->port = $port;
}

public function getEntityManager()
{
    $isDevMode = true;
    $paths = [APP_DIR . 'classes/Entities'];
    $connectionCredentials = $this->getConnectionCredentials();

    $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
    $entityManager = \Doctrine\ORM\EntityManager::create($connectionCredentials, $config);
    $connection = $entityManager->getConnection();

    $sqlSchemaManager = new SQLServerSchemaManager($connection);
    $sqlSchemaManager->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

    return $entityManager;
}

private function getConnectionCredentials()
{
    return [
        'driver'   => 'pdo_mysql',
        'user'     => $this->userName,
        'password' => $this->password,
        'dbname'   => $this->database,
        'host'     => $this->host,
        'port'     => $this->port
    ];
}

When my code needs to instantiate an entityManager I am getting this error:

Entry "ABCData\ABCDataAPI\CronJob" cannot be resolved: Entry "ABCData\DataStructure\Save" cannot be resolved: Entry "Doctrine\ORM\EntityManager" cannot be resolved: Parameter $username of __construct() has no value defined or guessable
Full definition:
Object (
    class = ABCData\Database\Database
    lazy = false
    __construct(
        $username = #UNDEFINED#
        $password = #UNDEFINED#
        $database = #UNDEFINED#
        $host = #UNDEFINED#
        $port = (default value) 3306
    )
    getEntityManager(

    )
)

It seems its not using the previously defined database thing.

What do I wrong?

Ok, I misunderstood the documentation.

This is what I did. If you have better idea, please tell me:

\Doctrine\ORM\EntityManager::class => DI\factory(function (\ABCData\Database\Database $database) {
    return $database->getEntityManager();
}),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM