|
| 1 | +# User Interface |
| 2 | + |
| 3 | +The package is shipped with few routes that will allow you and your users, to watch for `JobExecution`. |
| 4 | + |
| 5 | +<img src="images/bootstrap4-list.png" alt="Bootstrap 4 - List action" width="23%"> <img src="images/bootstrap4-details.png" alt="Bootstrap 4 - Detail : Information" width="23%"> <img src="images/bootstrap4-children.png" alt="Bootstrap 4 - Detail : Children" width="23%"> <img src="images/bootstrap4-warnings.png" alt="Bootstrap 4 - Detail : Warnings" width="23%"> |
| 6 | + |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +For the UI to be enabled, it is required that you install some dependencies: |
| 11 | +```shell |
| 12 | +composer require symfony/translation symfony/twig-bundle |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +## Configuration |
| 17 | + |
| 18 | +The UI is disabled by default, you must enable it explicitely: |
| 19 | +```yaml |
| 20 | +# config/packages/yokai_batch.yaml |
| 21 | +yokai_batch: |
| 22 | + ui: |
| 23 | + enabled: true |
| 24 | +``` |
| 25 | +
|
| 26 | +You will also need to import bundle routes: |
| 27 | +```yaml |
| 28 | +# config/routes/yokai_batch.yaml |
| 29 | +_yokai_batch: |
| 30 | + resource: "@YokaiBatchBundle/Resources/routing/ui.xml" |
| 31 | +``` |
| 32 | +
|
| 33 | +### Templating |
| 34 | +
|
| 35 | +The templating service is used by the [JobController](../src/UserInterface/Controller/JobController.php) to render its templates. |
| 36 | +It's a wrapper around [Twig](https://twig.symfony.com/), for you to control templates used, and variables passed. |
| 37 | +
|
| 38 | +> By default |
| 39 | +> - the templating will find templates like `@YokaiBatch/bootstrap4/*.html.twig` |
| 40 | +> - the template base view will be `base.html.twig` |
| 41 | + |
| 42 | +You can configure a prefix for all templates: |
| 43 | +```yaml |
| 44 | +# config/packages/yokai_batch.yaml |
| 45 | +yokai_batch: |
| 46 | + ui: |
| 47 | + templating: |
| 48 | + prefix: 'batch/job/' |
| 49 | +``` |
| 50 | +> With this configuration, we will look for templates like `batch/job/*.html.twig`. |
| 51 | + |
| 52 | +You can also configure the name of the base template for the root views of that bundle: |
| 53 | +```yaml |
| 54 | +# config/packages/yokai_batch.yaml |
| 55 | +yokai_batch: |
| 56 | + ui: |
| 57 | + templating: |
| 58 | + base_template: 'layout.html.twig' |
| 59 | +``` |
| 60 | +> With this configuration, the template base view will be `layout.html.twig`. |
| 61 | + |
| 62 | +If these are not enough, or if you need to add more variables to context, you can configure a service: |
| 63 | +```yaml |
| 64 | +# config/packages/yokai_batch.yaml |
| 65 | +yokai_batch: |
| 66 | + ui: |
| 67 | + templating: |
| 68 | + service: 'App\Batch\AppTemplating' |
| 69 | +``` |
| 70 | + |
| 71 | +And create the class that will cover the templating: |
| 72 | +```php |
| 73 | +<?php |
| 74 | +
|
| 75 | +declare(strict_types=1); |
| 76 | +
|
| 77 | +namespace App\Batch; |
| 78 | +
|
| 79 | +use Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\TemplatingInterface; |
| 80 | +
|
| 81 | +final class AppTemplating implements TemplatingInterface |
| 82 | +{ |
| 83 | + public function name(string $name): string |
| 84 | + { |
| 85 | + return "another-$name"; // change $name if you want |
| 86 | + } |
| 87 | +
|
| 88 | + public function context(array $context): array; |
| 89 | + { |
| 90 | + return \array_merge($context, ['foo' => 'bar']); // add variables to $context if you want |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +> **Note** You can also use the `Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\ConfigurableTemplating` that will cover both prefix and static variables at construction. |
| 96 | + |
| 97 | + |
| 98 | +### Filtering |
| 99 | + |
| 100 | +The `JobExecution` list includes a filter form, but you will need another optional dependency: |
| 101 | +```shell |
| 102 | +composer require symfony/form |
| 103 | +``` |
| 104 | + |
| 105 | +### Security |
| 106 | + |
| 107 | +There is no access control over `JobExecution` by default, you will need another optional dependency: |
| 108 | +```shell |
| 109 | +composer require symfony/security-bundle |
| 110 | +``` |
| 111 | + |
| 112 | +Every security attribute the bundle is using is configurable: |
| 113 | +```yaml |
| 114 | +# config/packages/yokai_batch.yaml |
| 115 | +yokai_batch: |
| 116 | + ui: |
| 117 | + security: |
| 118 | + attributes: |
| 119 | + list: ROLE_JOB_LIST # defaults to IS_AUTHENTICATED |
| 120 | + view: ROLE_JOB_VIEW # defaults to IS_AUTHENTICATED |
| 121 | + traces: ROLE_JOB_TRACES # defaults to IS_AUTHENTICATED |
| 122 | + logs: ROLE_JOB_LOGS # defaults to IS_AUTHENTICATED |
| 123 | +``` |
| 124 | + |
| 125 | +Optionally, you can register a voter for these attributes. |
| 126 | +This is especially useful if you need different access control rules per `JobExecution`. |
| 127 | +```php |
| 128 | +<?php |
| 129 | +
|
| 130 | +declare(strict_types=1); |
| 131 | +
|
| 132 | +namespace App\Security; |
| 133 | +
|
| 134 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 135 | +use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
| 136 | +use Yokai\Batch\JobExecution; |
| 137 | +
|
| 138 | +final class JobVoter extends Voter |
| 139 | +{ |
| 140 | + protected function supports(string $attribute, mixed $subject): bool |
| 141 | + { |
| 142 | + return \str_starts_with($attribute, 'JOB_'); |
| 143 | + } |
| 144 | +
|
| 145 | + /** |
| 146 | + * @param JobExecution|null $subject |
| 147 | + */ |
| 148 | + protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool |
| 149 | + { |
| 150 | + // TODO: Implement voteOnAttribute() method. |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | +## Integration with SonataAdminBundle |
| 157 | + |
| 158 | +If you are on a [SonataAdmin](https://symfony.com/bundles/SonataAdminBundle/current/index.html) project. |
| 159 | +The bundle got you covered with a dedicated templating services and templates. |
| 160 | + |
| 161 | +<img src="images/sonata-list.png" alt="Sonata - List action" width="23%"> <img src="images/sonata-details.png" alt="Sonata - Detail : Information" width="23%"> <img src="images/sonata-children.png" alt="Sonata - Detail : Children" width="23%"> <img src="images/sonata-warnings.png" alt="Sonata - Detail : Warnings" width="23%"> |
| 162 | + |
| 163 | +```shell |
| 164 | +composer require sonata-project/admin-bundle |
| 165 | +``` |
| 166 | + |
| 167 | +```yaml |
| 168 | +# config/packages/yokai_batch.yaml |
| 169 | +yokai_batch: |
| 170 | + ui: |
| 171 | + templating: sonata |
| 172 | +``` |
| 173 | +> With this configuration, we will look for templates like `@YokaiBatch/sonata/*.html.twig`. |
| 174 | + |
| 175 | + |
| 176 | +## Customizing templates |
| 177 | + |
| 178 | +You can override templates like [described it Symfony's documentation](https://symfony.com/doc/current/bundles/override.html). |
| 179 | +Examples: |
| 180 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/list.html.twig` |
| 181 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/show/_parameters.html.twig` |
| 182 | + |
| 183 | +But you can also register job name dedicated templates if you need some specific view for one of your jobs: |
| 184 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/show/{job name}/_children-executions.html.twig` |
| 185 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/show/{job name}/_failures.html.twig` |
| 186 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/show/{job name}/_general.html.twig` |
| 187 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/show/{job name}/_information.html.twig` |
| 188 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/show/{job name}/_parameters.html.twig` |
| 189 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/show/{job name}/_summary.html.twig` |
| 190 | +- `templates/bundles/YokaiBatchBundle/bootstrap4/show/{job name}/_warnings.html.twig` |
| 191 | + |
| 192 | +## On the same subject |
| 193 | + |
| 194 | +- [What is a job execution storage ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/job-execution-storage.md) |
| 195 | +- [What is a job ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/job.md) |
| 196 | +- [What is a job launcher ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/job-launcher.md) |
0 commit comments