phphleb/draft

This package is abandoned and no longer maintained. No replacement package was suggested.

Generator of classes from drafts for the HLEB framework

v1.0.5 2022-12-28 14:45 UTC

This package is auto-updated.

Last update: 2024-02-28 17:55:43 UTC


README

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 (RU)

Draft Instances is an experimental technique, as well as some others in HLEB framework encompassing this library. Unsuitable for using recklessly. If desired, the library may be connected separately (generation is implemented in GeneratingTask class).

Installation

$ composer require phphleb/draft

Deployment

$ 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.

$ 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:

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
/* *//**<-@Description*/
class TestDraftClass/**<-@ClassName*/
{
  private const VALUE = null/**<-@Value*/;
  public function get(): void/**<-@ReturnType*/
  {
     return /**<-@ActionName*/(self::VALUE);
  }
}

3 After starting the generation, two working classes are obtained:

<?php
/* Demo A class */
class AClass
{
  private const VALUE = 'now';
  public function get(): \DateTimeInterface
  {
     return new \DateTime(self::VALUE);
  }
}
<?php
/* Demo B class */
class BClass
{
  private const VALUE = [100,500];
  public function get(): string
  {
     return implode(self::VALUE);
  }
}

The substitution was performed according to markers /**<-@...*/, then the value on the left to the marker, before the gap char, was taken, and all this was substituted with the suitable value from the class configuration. Only this rule is to be taken into account, on creating your own classes-"drafts" and their settings.

By default, some examples illustrating generation capabilities were created in this library (services.php file and DraftInstances folder with "drafts").

Switch to testing

Allows you to reassign classes from another folder (with stubs) for the entire project.

$ php console drafts/generating-task --update-all /app/Optional/tests/services.php
$ composer dump-autoload

After that, you can run tests.

Attention! Only for the development server. Then you need to switch back to default.

Updating the library

$ composer update phphleb/draft
$ php console phphleb/draft --add
$ php console drafts/generating-task --update-all
$ composer dump-autoload

License: MIT PHP PHP PHP