每个项目中都会用到的东西


README

Build Status Code Coverage Scrutinizer Code Quality
每个项目中都会用到的东西。

数据库凭证

通过传递一个数组来设置数据库凭证

use Del\Common\Config\DbCredentials;

$credentials = new DbCredentials([
    'driver' => 'pdo_mysql',
    'dbname' => 'delboy1978uk',
    'user' => 'dbuser',
    'password' => '[123456]',
]);

DIC 容器工厂服务

use Del\Common\ContainerService;

$containerSvc = ContainerService::getInstance();

服务方法

$containerSvc->setDbCredentials($credentials); // Do this before getContainer() to configure the DBAL Connection
$containerSvc->addEntityPath('path/to/entities'); // You can add multiple paths to get Entities from different packages
$containerSvc->registerToContainer($registrationInterface); // See below

容器注册接口

您可以在自己的包中创建一个类来注册任何要放入容器的定义。只需实现 Barnacle\RegistrationInterface。例如。

namespace My\Config\Container;


use Barnacle\Container;
use Barnacle\RegistrationInterface;
use Doctrine\ORM\EntityManager;
use My\Repository\Dog as DogRepository;

class DogPackage implements RegistrationInterface
{
    /**
     * @param Container $c
     */
    public function addToContainer(Container $c)
    {
        $c['repository.dog'] = $c->factory(function ($c) {
            /** @var EntityManager $em */
            $em = $c['doctrine.entity_manager'];
            /** @var DogRepository $repo */
            $repo = $em->getRepository('My\Entity\Dog');
            return $repo; 
        });
    }
}

Pimple 容器

一个依赖注入容器。现在它包含了一个配置好的 Doctrine 2 实体管理器

$container = $containerSvc->getContainer();
$em = $container['doctrine.entity_manager'];
$dogRepo = $container['repository.dog'];

值对象

有几个值对象扩展了 Del\Common\Value\AbstractValue。

use Del\Common\Value\DecimalValue;
use Del\Common\Value\IntValue;
use Del\Common\Value\StringValue;

$money = new DecimalValue(123.45);
$text = new StringValue('Hooray');
$int = new IntValue(500);

echo $money->getValue();
echo $text->getValue();
echo $int->getValue();