"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Small Clean Application

Small Clean Application

Published on 2024-08-06
Browse:772

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.

Git : https://git.small-project.dev/lib/small-clean-application
Packagist : https://packagist.org/packages/small/clean-application

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 :

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 :

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 :

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 :

before;

    }

}

And his interface :

And here is the response implementation :

status;
    }

}

And his interface :

Release Statement This article is reproduced at: https://dev.to/sebk69/small-clean-application-4c1c?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3