定制 small/clean-application 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

small/clean-application

最新稳定版本:1.3.1

Composer 安装命令:

composer require small/clean-application

包简介

This project is a set of class to manage dependency injection of application's part of a clean architecture app, independently of the framework used.

README 文档

README

Small Clean Application

  

This project is a set of class to manage dependency injection of application's part of a clean architecture app, independently of the framework used.

Install

composer require small/clean-application

Parameters

Parameters are managed to automatically inject them in UseCase constructor.

You can set parameters through the facade static object :

\Small\CleanApplication\Facade::setParameter('test', [
    'host' => 'http://clean.com',
    'port' => 80
]);

You can also get them through the facade :

echo \Small\CleanApplication\Facade::getParameter('test.host');

Output :

http://clean.com

UseCase class

Simple case

A use case is a class materialization of use case that implement Small\CleanApplication\Contract\UseCaseInterface.

For example, here is simply use case that return a string :

<?php

namespace Small\CleanApplication\Test\Feature\Fixture\UseCase;

use Small\CleanApplication\Test\Feature\Fixture\Interface\TestResponseInterface;

class TestUseCase implements \Small\CleanApplication\Contract\UseCaseInterface
{
    public function execute($request): TestResponseInterface
    {

        return new TestResponse('a');

    }


}

You can use it using facade :

use Small\CleanApplication\Test\Feature\Fixture\UseCase\TestUseCase;
use \Small\CleanApplication\Test\Feature\Fixture\UseCase\TestRequest;

echo \Small\CleanApplication\Facade::execute(TestUseCase::class, new TestRequest());

Output :

a

Injecting another use case in your use case

You can inject another use case in the use case constructor :

<?php

namespace Small\CleanApplication\Test\Feature\Fixture\UseCase;

use Small\CleanApplication\Contract\UseCaseInterface;
use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyRequestInterface;
use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyResponseInterface;

class TestDependencyUseCase implements UseCaseInterface
{

    public function __construct(
        protected TestUseCase $testUseCase,
    ) {}

    public function execute($request): TestDependencyResponseInterface
    {

        return new TestDependencyResponse(
            $request->getBefore() . $this->testUseCase->execute($request)->getStatus()
        );

    }

}

The property testUseCase will automatically be created as TestUseCase object.

Injecting parameters in your use case

You can inject parameters in your use case by typing and naming property in your use case constructor :

<?php

namespace Small\CleanApplication\Test\Feature\Fixture\UseCase;

use Small\CleanApplication\Contract\UseCaseInterface;
use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyRequestInterface;
use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyResponseInterface;

class TestDependencyParamUseCase implements UseCaseInterface
{

    public function __construct(
        protected string $testUseCase_param,
        protected TestUseCase $testUseCase,
    ) {}

    public function execute($request): TestDependencyResponseInterface
    {

        if (!$request instanceof TestDependencyRequestInterface) {
            throw new \Exception('Bad request');
        }

        return new TestDependencyResponse(
            $this->testUseCase_param . $request->getBefore() . $this->testUseCase->execute($request)->getStatus()
        );

    }

}

The underscrore ('_') separate array keys of parameters structure. Here is an example matching with the $testUseCase_param :

\Small\CleanApplication\Facade::setParameter('testUseCase', ['param' => 'p']);

Interfaces

Three interface structure your code :

  • Small\CleanApplication\Contract\UseCaseInterface : All your use cases must implement this interface
  • Small\CleanApplication\Contract\Request : All your use case requests must implement this interface
  • Small\CleanApplication\Contract\Response : All your use case response must implement this interface

Here's our TestDependency example request class :

<?php

namespace Small\CleanApplication\Test\Feature\Fixture\UseCase;

use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyRequestInterface;

readonly class TestDependencyRequest implements TestDependencyRequestInterface
{
    
    public function __construct(
        protected string $before,
    ) {}
    
    public function getBefore(): string
    {
        
        return $this->before;
        
    }

}

And his interface :

<?php

namespace Small\CleanApplication\Test\Feature\Fixture\Interface;

use Small\CleanApplication\Contract\RequestInterface;

interface TestDependencyRequestInterface extends RequestInterface
{

    public function getBefore(): string;

}

And here is the response implementation :

<?php

namespace Small\CleanApplication\Test\Feature\Fixture\UseCase;

use Small\CleanApplication\Test\Feature\Fixture\Interface\TestDependencyResponseInterface;

readonly class TestDependencyResponse implements TestDependencyResponseInterface
{

    public function __construct(
        protected string $status,
    ) {}

    public function getStatus(): string
    {
        return $this->status;
    }

}

And his interface :

<?php

namespace Small\CleanApplication\Test\Feature\Fixture\Interface;

use Small\CleanApplication\Contract\ResponseInterface;

interface TestDependencyResponseInterface extends ResponseInterface
{

    public function getStatus(): string;

}

Copyright

This project is under MIT license

统计信息

  • 总下载量: 100
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-26