Skip to content

Commit 2f13e7e

Browse files
#37 Refactor models to allow overriding:
- replace php annotated entities by xml mapping/validations. Add Interfaces. - add class entries on configuration for models - rework install documentation with a Doctrine ORM Configuration chapter - rework repositories and other services using model classNames & Interfaces
1 parent e575297 commit 2f13e7e

39 files changed

+714
-270
lines changed

config/doctrine/LogRecord.orm.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
3+
<mapped-superclass name="CleverAge\UiProcessBundle\Entity\LogRecord">
4+
<indexes>
5+
<index name="idx_log_record_level" columns="level" />
6+
<index name="idx_log_record_created_at" columns="created_at" />
7+
</indexes>
8+
9+
<id name="id" column="id" type="integer">
10+
<generator strategy="AUTO" />
11+
</id>
12+
<field name="channel" type="string" length="64" />
13+
<field name="level" type="integer" />
14+
<field name="message" type="string" length="512" />
15+
<field name="context" type="json" />
16+
<field name="createdAt" type="datetime_immutable" />
17+
<many-to-one field="processExecution" target-entity="CleverAge\UiProcessBundle\Entity\ProcessExecutionInterface">
18+
<join-column name="process_execution_id" referenced-column-name="id" on-delete="CASCADE" nullable="false" />
19+
</many-to-one>
20+
</mapped-superclass>
21+
</doctrine-mapping>
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
3+
<mapped-superclass name="CleverAge\UiProcessBundle\Entity\ProcessExecution">
4+
<indexes>
5+
<index name="idx_process_execution_code" columns="code" />
6+
<index name="idx_process_execution_start_date" columns="start_date" />
7+
</indexes>
8+
9+
<id name="id" column="id" type="integer">
10+
<generator strategy="AUTO" />
11+
</id>
12+
<field name="code" type="string" length="255" />
13+
<field name="startDate" type="datetime_immutable"/>
14+
<field name="endDate" type="datetime_immutable" nullable="true" />
15+
<field name="status" type="string" enum-type="CleverAge\UiProcessBundle\Entity\Enum\ProcessExecutionStatus" />
16+
<field name="report" type="json" />
17+
<field name="context" type="json" nullable="true" />
18+
<field name="logFilename" type="string" length="255" />
19+
</mapped-superclass>
20+
</doctrine-mapping>
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
3+
<mapped-superclass name="CleverAge\UiProcessBundle\Entity\ProcessSchedule">
4+
<id name="id" column="id" type="integer">
5+
<generator strategy="AUTO" />
6+
</id>
7+
<field name="process" type="string" length="255" />
8+
<field name="type" type="string" length="6" enum-type="CleverAge\UiProcessBundle\Entity\Enum\ProcessScheduleType" />
9+
<field name="expression" type="string" length="255" />
10+
<field name="input" type="text" nullable="true" />
11+
<field name="context" type="json" />
12+
</mapped-superclass>
13+
</doctrine-mapping>

config/doctrine/User.orm.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
3+
<mapped-superclass name="CleverAge\UiProcessBundle\Entity\User" table="process_user">
4+
<indexes>
5+
<index name="idx_process_user_email" columns="email" />
6+
</indexes>
7+
8+
<id name="id" column="id" type="integer">
9+
<generator strategy="AUTO" />
10+
</id>
11+
<field name="email" type="string" length="255" unique="true" />
12+
<field name="firstname" type="string" length="255" nullable="true" />
13+
<field name="lastname" type="string" length="255" nullable="true" />
14+
<field name="roles" type="json" />
15+
<field name="password" type="string" length="255" nullable="true" />
16+
<field name="timezone" type="string" length="255" nullable="true" />
17+
<field name="locale" type="string" length="255" nullable="true" />
18+
<field name="token" type="string" length="255" nullable="true" />
19+
</mapped-superclass>
20+
</doctrine-mapping>

config/services/command.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ services:
55
tags:
66
- { name: console.command }
77
arguments:
8+
- '%cleverage_ui_process.entity.user.class%'
89
- '@validator'
910
- '@security.user_password_hasher'
1011
- '@doctrine.orm.entity_manager'

config/services/event_subscriber.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ services:
55
tags:
66
- { name: 'kernel.event_subscriber' }
77
arguments:
8+
- '%cleverage_ui_process.entity.process_execution.class%'
89
- '@cleverage_ui_process.monolog_handler.process'
910
- '@cleverage_ui_process.monolog_handler.doctrine_process'
1011
- '@cleverage_ui_process.manager.process_execution'

config/services/monolog_handler.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ services:
55
calls:
66
- [ setEntityManager, [ '@doctrine.orm.entity_manager' ] ]
77
- [ setProcessExecutionManager, [ '@cleverage_ui_process.manager.process_execution' ] ]
8+
- [ setLogRecordClassName, [ '%cleverage_ui_process.entity.log_record.class%' ] ]
89
CleverAge\UiProcessBundle\Monolog\Handler\DoctrineProcessHandler:
910
alias: cleverage_ui_process.monolog_handler.doctrine_process
1011

config/services/repository.yaml

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@ services:
44
public: false
55
arguments:
66
- '@doctrine.orm.entity_manager'
7+
- '%cleverage_ui_process.entity.process_execution.class%'
8+
- '%cleverage_ui_process.entity.log_record.class%'
79

810
cleverage_ui_process.repository.process_schedule:
911
class: CleverAge\UiProcessBundle\Repository\ProcessScheduleRepository
1012
public: false
1113
arguments:
12-
- '@doctrine'
14+
- '@doctrine.orm.entity_manager'
15+
- '%cleverage_ui_process.entity.process_schedule.class%'
1316

17+
cleverage_ui_process.repository.user:
18+
class: CleverAge\UiProcessBundle\Repository\UserRepository
19+
public: false
20+
arguments:
21+
- '@doctrine.orm.entity_manager'
22+
- '%cleverage_ui_process.entity.user.class%'

config/services/security.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ services:
33
class: CleverAge\UiProcessBundle\Security\HttpProcessExecutionAuthenticator
44
public: false
55
arguments:
6-
- '@doctrine.orm.entity_manager'
6+
- '@cleverage_ui_process.repository.user'
77

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping">
3+
<class name="CleverAge\UiProcessBundle\Entity\ProcessSchedule">
4+
<property name="process">
5+
<constraint name="CleverAge\UiProcessBundle\Validator\IsValidProcessCode" />
6+
</property>
7+
<property name="expression">
8+
<constraint name="When">
9+
<option name="expression">this.getType().value == "cron"</option>
10+
<option name="constraints">
11+
<constraint name="CleverAge\UiProcessBundle\Validator\CronExpression" />
12+
</option>
13+
</constraint>
14+
<constraint name="When">
15+
<option name="expression">this.getType().value == "every"</option>
16+
<option name="constraints">
17+
<constraint name="CleverAge\UiProcessBundle\Validator\EveryExpression" />
18+
</option>
19+
</constraint>
20+
</property>
21+
</class>
22+
</constraint-mapping>

docs/index.md

+76-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,89 @@ Remember to add the following line to `config/bundles.php` (not required if Symf
1919
CleverAge\UiProcessBundle\CleverAgeUiProcessBundle::class => ['all' => true],
2020
```
2121

22+
## Doctrine ORM Configuration
23+
24+
Add these in the config mapping definition (or enable [auto_mapping](https://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration)):
25+
26+
```yaml
27+
# config/packages/doctrine.yaml
28+
29+
doctrine:
30+
orm:
31+
mappings:
32+
CleverAgeUiProcessBundle: ~
33+
```
34+
35+
And then create the corresponding entities:
36+
37+
```php
38+
// src/Entity/LogRecord.php
39+
40+
use Doctrine\ORM\Mapping as ORM;
41+
use CleverAge\UiProcessBundle\Entity\LogRecord as BaseLogRecord;
42+
43+
#[ORM\Entity]
44+
#[ORM\Table]
45+
class LogRecord extends BaseLogRecord
46+
{
47+
}
48+
```
49+
50+
```php
51+
// src/Entity/ProcessExecution.php
52+
53+
use Doctrine\ORM\Mapping as ORM;
54+
use CleverAge\UiProcessBundle\Entity\ProcessExecution as BaseProcessExecution;
55+
56+
#[ORM\Entity]
57+
#[ORM\Table]
58+
class ProcessExecution extends BaseProcessExecution
59+
{
60+
}
61+
```
62+
63+
```php
64+
// src/Entity/ProcessSchedule.php
65+
66+
use Doctrine\ORM\Mapping as ORM;
67+
use CleverAge\UiProcessBundle\Entity\ProcessSchedule as BaseProcessSchedule;
68+
69+
#[ORM\Entity]
70+
#[ORM\Table]
71+
class ProcessSchedule extends BaseProcessSchedule
72+
{
73+
}
74+
```
75+
76+
```php
77+
// src/Entity/User.php
78+
79+
use Doctrine\ORM\Mapping as ORM;
80+
use CleverAge\UiProcessBundle\Entity\User as BaseUser;
81+
82+
#[ORM\Entity]
83+
#[ORM\Table(name: 'process_user')]
84+
class User extends BaseUser
85+
{
86+
}
87+
```
88+
89+
So, update your schema:
90+
91+
```bash
92+
bin/console doctrine:schema:update --force
93+
```
94+
or use [DoctrineMigrationsBundle](https://github.com/doctrine/DoctrineMigrationsBundle)
95+
96+
And create a User using `cleverage:ui-process:user-create` console.
97+
2298
## Import routes
2399

24100
```yaml
25101
ui-process-bundle:
26102
resource: '@CleverAgeUiProcessBundle/src/Controller'
27103
type: attribute
28104
```
29-
* Run doctrine migration
30-
* Create a user using `cleverage:ui-process:user-create` console.
31105
32106
Now you can access UI Process via http://your-domain.com/process
33107

src/CleverAgeUiProcessBundle.php

+10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@
1313

1414
namespace CleverAge\UiProcessBundle;
1515

16+
use CleverAge\UiProcessBundle\DependencyInjection\Compiler\ResolveTargetEntityPass;
17+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
1619
use Symfony\Component\HttpKernel\Bundle\Bundle;
1720

1821
class CleverAgeUiProcessBundle extends Bundle
1922
{
23+
public function build(ContainerBuilder $container)
24+
{
25+
parent::build($container);
26+
27+
$container->addCompilerPass(new ResolveTargetEntityPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1000);
28+
}
29+
2030
public function getPath(): string
2131
{
2232
return \dirname(__DIR__);

src/Command/UserCreateCommand.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace CleverAge\UiProcessBundle\Command;
1515

16-
use CleverAge\UiProcessBundle\Entity\User;
16+
use CleverAge\UiProcessBundle\Entity\UserInterface;
1717
use Doctrine\ORM\EntityManagerInterface;
1818
use Symfony\Component\Console\Attribute\AsCommand;
1919
use Symfony\Component\Console\Command\Command;
@@ -35,7 +35,11 @@
3535
)]
3636
class UserCreateCommand extends Command
3737
{
38+
/**
39+
* @param class-string<UserInterface> $userClassName
40+
*/
3841
public function __construct(
42+
private readonly string $userClassName,
3943
private readonly ValidatorInterface $validator,
4044
private readonly UserPasswordHasherInterface $passwordEncoder,
4145
private readonly EntityManagerInterface $em,
@@ -54,7 +58,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5458
$output
5559
);
5660

57-
$user = new User();
61+
/** @var UserInterface $user */
62+
$user = new $this->userClassName();
5863
$user->setEmail($username);
5964
$user->setRoles(['ROLE_USER', 'ROLE_ADMIN']);
6065
$user->setPassword($this->passwordEncoder->hashPassword($user, $password));

src/Controller/Admin/Process/LaunchAction.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace CleverAge\UiProcessBundle\Controller\Admin\Process;
1515

1616
use CleverAge\ProcessBundle\Exception\MissingProcessException;
17-
use CleverAge\UiProcessBundle\Entity\User;
17+
use CleverAge\UiProcessBundle\Entity\UserInterface;
1818
use CleverAge\UiProcessBundle\Form\Type\LaunchType;
1919
use CleverAge\UiProcessBundle\Manager\ProcessConfigurationsManager;
2020
use CleverAge\UiProcessBundle\Message\ProcessExecuteMessage;
@@ -130,9 +130,9 @@ protected function dispatch(string $processCode, mixed $input = null, array $con
130130
$this->messageBus->dispatch($message);
131131
}
132132

133-
protected function getUser(): ?User
133+
protected function getUser(): ?UserInterface
134134
{
135-
/** @var User $user */
135+
/** @var UserInterface $user */
136136
$user = parent::getUser();
137137

138138
return $user;

src/Controller/Admin/ProcessExecutionCrudController.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use CleverAge\UiProcessBundle\Admin\Field\ContextField;
1717
use CleverAge\UiProcessBundle\Admin\Field\EnumField;
1818
use CleverAge\UiProcessBundle\Entity\ProcessExecution;
19-
use CleverAge\UiProcessBundle\Repository\ProcessExecutionRepository;
19+
use CleverAge\UiProcessBundle\Repository\ProcessExecutionRepositoryInterface;
2020
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
2121
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
2222
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
@@ -36,7 +36,7 @@
3636
class ProcessExecutionCrudController extends AbstractCrudController
3737
{
3838
public function __construct(
39-
private readonly ProcessExecutionRepository $processExecutionRepository,
39+
private readonly ProcessExecutionRepositoryInterface $processExecutionRepository,
4040
private readonly string $logDirectory,
4141
) {
4242
}
@@ -123,9 +123,8 @@ public function showLogs(AdminContext $adminContext): RedirectResponse
123123
return $this->redirect($url);
124124
}
125125

126-
public function downloadLogFile(
127-
AdminContext $context,
128-
): Response {
126+
public function downloadLogFile(AdminContext $context): Response
127+
{
129128
/** @var ProcessExecution $processExecution */
130129
$processExecution = $context->getEntity()->getInstance();
131130
$filepath = $this->getLogFilePath($processExecution);
@@ -149,7 +148,7 @@ public function configureFilters(Filters $filters): Filters
149148
private function getLogFilePath(ProcessExecution $processExecution): string
150149
{
151150
return $this->logDirectory.
152-
\DIRECTORY_SEPARATOR.$processExecution->code.
151+
\DIRECTORY_SEPARATOR.$processExecution->getCode().
153152
\DIRECTORY_SEPARATOR.$processExecution->logFilename
154153
;
155154
}

src/DependencyInjection/CleverAgeUiProcessExtension.php

+6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@ public function load(array $configs, ContainerBuilder $container): void
3333

3434
$configuration = new Configuration();
3535
$config = $this->processConfiguration($configuration, $configs);
36+
3637
$container->getDefinition(UserCrudController::class)
3738
->setArgument('$roles', array_combine($config['security']['roles'], $config['security']['roles']));
3839
$container->getDefinition('cleverage_ui_process.monolog_handler.process')
3940
->addMethodCall('setReportIncrementLevel', [$config['logs']['report_increment_level']]);
4041
$container->getDefinition(ProcessDashboardController::class)
4142
->setArgument('$logoPath', $config['design']['logo_path']);
43+
44+
$container->setParameter('cleverage_ui_process.entity.log_record.class', $config['class']['log_record']);
45+
$container->setParameter('cleverage_ui_process.entity.process_execution.class', $config['class']['process_execution']);
46+
$container->setParameter('cleverage_ui_process.entity.process_schedule.class', $config['class']['process_schedule']);
47+
$container->setParameter('cleverage_ui_process.entity.user.class', $config['class']['user']);
4248
}
4349

4450
/**

0 commit comments

Comments
 (0)