kassko/data-mapper-bundle

此包最新版本(v1.0.2)没有可用的许可证信息。

将 data-mapper 集成到 Symfony 项目中。


README

Total Downloads

此包将 data-mapper 组件集成到 Symfony 应用程序中。这是一个提供许多功能以将某些原始数据表示为对象的映射器。

要了解更多关于此组件及其用法,请阅读data-mapper 文档参考

Symfony 2 的安装

注意:

  • 当不兼容时使用第二个版本号
  • 第三个用于新功能
  • 第四个用于热修复
  • 第一个用于新 API 或从预发布版到发布版(从 0 到 1)

建议使用版本 0.14。版本 0.15 已不再维护。

您可以使用 composer 安装此库,以下是一个良好的需求

composer require kassko/data-mapper-bundle:"~0.14.4"

app/AppKernel.php 中注册此包

public function registerBundles()
{
    $bundles = array(
        new Kassko\Bundle\DataMapperBundle\KasskoDataMapperBundle(),
        new Kassko\Bundle\ClassResolverBundle\KasskoClassResolverBundle(),
    );
}

DataMapper 服务

从控制器获取服务

$this->get('kassko_data_mapper');

它代表一个 Kassko\DataMapper\DataMapper 实例。要了解更多关于此组件及其用法,请阅读data-mapper 文档参考

配置参考

kassko_data_mapper:
    mapping:
        default_resource_type: annotations # Default is "annotations" or other type (1).
        default_resource_dir: # Optional.
        default_provider_method: # Optional.
        bundles: #optional section
            some_bundle:
                resource_type: annotations # Default is "annotations" or other type (1).
                resource_dir: # The resource dir of the given bundle.
                provider_method: ~ # Required. Default value is null.
                objects: # Optional section.
                    some_object:
                        resource_type: # Optional.
                        resource_path: # Optional. The resource directory with the resource name. If not defined, data-mapper fallback to resource_name and prepend to it resource_dir (or default_resource_dir). So if resource_path is not defined, case resource_name and resource_dir (or default_resource_dir) must be defined.
                        resource_name: # Optional. Only the resource name (so without the directory).
                        provider_method: # Optional. Override default_provider_method.
                        object_class: # Required (full qualified object class name).
    cache:
        metadata_cache: # Optional section
            class: # Optional.
            id: # Optional.
            life_time: # Default is 0
            is_shared: # Default is false
            adapter_class: # Default is "Kassko\Bundle\DataMapperBundle\Adapter\Cache\DoctrineCacheAdapter"
        result_cache: # Optional section and same as metadata_cache
    logger_service: # Optional. A logger service name. Il will be used for logging in data-mapper component.

(1) 可用的类型有注释、yaml、php、php_file、yaml_file。也许还有其他,请随意添加自定义映射加载器。

表达式语言集成

表达式语言服务

添加一个提供者

<service id="my_provider" class="Kassko\Sample\SomeExpressionFunctionProvider">
    <tag name="kassko_data_mapper.expression_function_provider" variable_key="container" variable_value="service_container"/>
</service>

在上面的代码中,容器在您的提供者中可用。您可以使用它

use Kassko\DataMapper\Expression\ExpressionFunction;
use Kassko\DataMapper\Expression\ExpressionFunctionProviderInterface;

class ExpressionFunctionProvider implements ExpressionFunctionProviderInterface
{
    public function getFunctions()
    {
        return [
            new ExpressionFunction(
                'granted',
                function ($arg) {
                    return sprintf('container.get(%s)', $arg);
                }, 
                function (array $context, $value) {
                    return $context['container']->get($value);
                }
            ),
        ];
    }
}

对象监听器

data-mapper 需要能够从其完全限定类名中检索对象监听器。为了做到这一点,您必须将您的对象监听器注册为服务,并使用 kassko_data_mapper.listener 标签。

要了解更多关于对象监听器,请阅读data-mapper 文档参考

自定义加载器

DataMapper 提供三种映射格式:annotationsyamlphp。但您可以使用自定义映射加载器。

有关如何实现您自己的加载器的更多详细信息,请阅读data-mapper 文档参考

在不注入的情况下使用服务在持久对象中

您需要将其添加到注册表中。您可以通过这种方式完成。

标记您的服务

<service id="logger">
    <tag name="kassko_data_mapper.registry_item" key="logger">
</service>

然后您可以从您的持久对象中获取您的服务

trait LoggableTrait
{
    private function getLogger()
    {
        return Registry::getInstance()['logger'];
    }
}
class Person
{
    use LoggableTrait;

    private $id;
    private $name;
    private $address;

    public function getName()
    {
        if (! isset($this->address)) {
            $this->getLogger()->warning(sprintf('No address for %s', $this->name));
        }
    }
}