ulrack/services

此包已被弃用,不再维护。作者建议使用grizz-it/services包。

PHP应用程序的服务实现。

3.4.0 2020-11-24 21:20 UTC

This package is auto-updated.

Last update: 2021-03-28 14:13:29 UTC


README

Build Status

Ulrack Services

此包包含服务实现。这些服务可以配置为项目以创建配置好的依赖注入层。服务将通过编译器运行以创建工厂的工作数据。工厂用于检索项目的组装对象。编译器和工厂可以通过创建编译器扩展和工厂扩展进行扩展。此外,现有的编译器和工厂可以通过钩子进行扩展和修改。

安装

要安装此包,请运行以下命令

composer require ulrack/services

用法

注册表

首先,需要创建ServiceRegistry。在编译之前,所有服务定义都注册在这个对象上。要创建ServiceRegistry,请使用以下代码片段

<?php

use Ulrack\Services\Component\Registry\ServiceRegistry;

$serviceRegistry = new ServiceRegistry();

然后可以使用add方法将服务定义注册。请参阅configuration/schema中的文件,以了解可以注册的所有内容。

<?php

use GrizzIt\Validator\Component\Logical\AlwaysValidator;

$serviceRegistry->add(
    'services',
    'foo',
    [
        'class' => AlwaysValidator::class,
        'parameters' => [
            'alwaysBool' => true
        ]
    ]
);

编译器

然后,可以使用以下代码片段创建编译器

<?php

use GrizzIt\ObjectFactory\Component\Analyser\ClassAnalyser;
use GrizzIt\ObjectFactory\Factory\ObjectFactory;
use Ulrack\Services\Component\Compiler\ServiceCompiler;
use GrizzIt\Storage\Component\ObjectStorage;
use GrizzIt\ObjectFactory\Component\Reflector\MethodReflector;

// The services storage in this example only exists during the execution.
// Use an alternative implementation of the StorageInterface to keep the compiled services.
$serviceStorage = new ObjectStorage();

$analysisStorage = new ObjectStorage();

$methodReflector = new MethodReflector();

$classAnalyser = new ClassAnalyser(
    $analysisStorage,
    $methodReflector
);

$objectFactory = new ObjectFactory($classAnalyser);

$serviceCompiler = new ServiceCompiler(
    $serviceRegistry,
    $serviceStorage,
    $objectFactory
);

编译器扩展

编译器用于重新格式化所有定义的代码,以便稍后在工厂中更快地运行。为了向编译器添加逻辑,需要添加扩展。为了支持服务,可以使用ServicesCompiler。扩展需要实现AbstractServiceCompilerExtension

如果服务已经处于最佳状态,还可以选择使用PassThroughCompiler

要添加ServicesCompiler,请使用以下代码片段

<?php

use Ulrack\Services\Component\Compiler\ServicesCompiler;

$serviceCompiler->addExtension(
    // This will be the key which is used in the service definitions for services.
    'services',
    ServicesCompiler::class,
    // This is the sort order of the extension, it is used to determine the order of execution.
    0,
    // Optionaly a validator implementing the ValidatorInterface can be provided here
    null,
    // Parameters can be provided to the extension here.
    []
);

编译器钩子

要向编译器添加钩子,可以使用以下代码片段

<?php

$serviceCompiler->addHook(
    // Note that the key must the same as the the compiler which needs to be hooked into.
    'services',
    MyHook::class,
    // The sort order to determine the order of execution.
    0,
    // Optional parameters for the hook.
    []
);

钩子需要实现AbstractServiceCompilerHook

创建工厂

一旦为编译器配置了一切,就可以用类似的方式创建工厂。

<?php

use Ulrack\Services\Factory\ServiceFactory;

$serviceFactory = new ServiceFactory(
    // The previously configured service compiler.
    $serviceCompiler,
    $objectFactory,
    $classAnalyser,
    $methodReflector
);

工厂扩展

工厂扩展必须实现AbstractServiceFactoryExtension。要添加services工厂,请使用以下代码片段:

<?php

use Ulrack\Services\Factory\Extension\ServicesFactory;

$serviceFactory->addExtension(
    // The key on which services are registered.
    'services',
    ServicesFactory::class,
    // Optional parameters.
    []
);

工厂钩子

添加工厂钩子的方法与工厂扩展类似。它必须实现AbstractServiceFactoryHook。然后要添加钩子,请使用以下代码片段:

<?php

$serviceFactory->addHook(
    // The key on which services are registered.
    'services',
    MyHook::class,
    // The sort order, used for the order of execution.
    0,
    // A set of optional parameters for the hook.
    []
);

使用工厂

配置完成后,可以使用工厂创建声明的服务的实例或检索配置的特定值。为此,请调用具有服务声明引用的create方法。

<?php

// The key is a combination of the scope of the required service and the name of the service.
$serviceFactory->create('services.foo')

这将返回在foo上注册的内容。

示例

要查看完整示例,请参阅示例目录。在根目录中运行以下命令以执行示例:

composer require ulrack/json-schema
php example/example.php

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTINGCODE_OF_CONDUCT

MIT许可证

版权所有 (c) GrizzIT

在此特此授予任何获得此软件及其相关文档副本(以下简称“软件”)的人免费使用该软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许将软件提供给其他人使用,前提是遵守以下条件:

上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。

软件按“原样”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担责任,无论这些责任是因合同、侵权或其他原因引起的,包括但不限于与软件或其使用或其它处理相关的任何责任。