An open API service indexing awesome lists of open source software.

https://github.com/phphleb/draft

Draft Instances - DI (Dependency injection) implementation
https://github.com/phphleb/draft

Last synced: about 1 year ago
JSON representation

Draft Instances - DI (Dependency injection) implementation

Awesome Lists containing this project

README

          

# Draft Instances

### Class generator using templates ("drafts")

This way is different from the traditional **DI (Dependency injection)**,
since it does not injects dependencies programmatically at runtime, but in advance,
by generating and changing classes according to settings. The created classes exist as files;
their correctness can be checked, dependencies are "visible" for IDE, so as testing is possible.

[Link to instructions](https://phphleb.ru/ru/v1/di/) (RU)

Draft Instances is an experimental technique, as well as some others in [HLEB](https://github.com/phphleb/hleb) framework encompassing this library.
*Unsuitable for using recklessly.* If desired, the library may be connected separately (generation is implemented in **GeneratingTask** class).

#### Installation
```bash
$ composer require phphleb/draft
```
#### Deployment
```bash
$ php console phphleb/draft --add
```
#### Generation
Generating classes and updating the autoloader. Every time when the settings or template classes are changed, this command is to be started.
```bash
$ php console drafts/generating-task --update-all
$ composer dump-autoload
```

The only distinction between the generated classes and the usual ones created by a developer is that the former are editable only from the "draft" or settings.

Using settings from one template ("draft"), you can create a lot of similar classes for different tasks. Each
"draft" is arbitrary in content, the principle of substituting settings into it is simple, and it is specified as follows.

`1` Array with settings:
```php
return [
'AClass' => [
'TestDraftClass' => [
'ActionName' => 'new \DateTime',
'Value' => '\'now\'',
'ReturnType' => '\DateTimeInterface',
'Description' => 'Demo A class'
]
],
'BClass' => [
'TestDraftClass' => [
'ActionName' => 'implode',
'Value' => '[100,500]',
'ReturnType' => 'string',
'Description' => 'Demo B class'
]
],
];
```
`2` Schematic template class **TestDraftClass.php** (default setting values for substitution are neutral-random to support syntax highlighting in IDE)
```php
=1.5.72-brightgreen)