Skip to content

Commit d2f5736

Browse files
committed
Issue #37: Added documentation for v4
Signed-off-by: alexmerlin <alex.merlin.1985@gmail.com>
1 parent 1de7ba3 commit d2f5736

10 files changed

Lines changed: 303 additions & 0 deletions

File tree

docs/book/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md

docs/book/v4/cache.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Caching the annotations
2+
3+
`dot-annotated-services` reads class annotations using [doctrine/annotations](https://github.com/doctrine/annotations) and caches them using [doctrine/cache](https://github.com/doctrine/cache).
4+
5+
6+
## Configuration
7+
8+
In order to cache annotations, you should register a service factory at key `AbstractAnnotatedFactory::CACHE_SERVICE` that should return a valid `Doctrine\Common\Cache\Cache` cache driver.
9+
See [Cache Drivers](https://github.com/doctrine/cache/tree/1.13.x/lib/Doctrine/Common/Cache) for available implementations offered by doctrine.
10+
11+
See below an example on how you can configure `dot-annotated-services` to cache annotations.
12+
You can add this configuration values to your application's Doctrine config file:
13+
14+
```php
15+
'annotations_cache_dir' => __DIR__ . '/../../data/cache/annotations',
16+
'dependencies' => [
17+
'factories' => [
18+
Dot\AnnotatedServices\Factory\AbstractAnnotatedFactory::CACHE_SERVICE => YourApp\Factory\AnnotationsCacheFactory::class,
19+
],
20+
];
21+
```
22+
where `AnnotationsCacheFactory` is a custom factory that needs to return a [Doctrine Cache Driver](https://github.com/doctrine/cache/tree/1.13.x/lib/Doctrine/Common/Cache):
23+
```php
24+
<?php
25+
26+
declare(strict_types=1);
27+
28+
namespace YourApp\Factory;
29+
30+
class AnnotationsCacheFactory
31+
{
32+
public function __invoke(Psr\Container\ContainerInterface $container)
33+
{
34+
return new Doctrine\Common\Cache\FilesystemCache($container->get('config')['annotations_cache_dir']);
35+
}
36+
}
37+
```

docs/book/v4/configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Configuration
2+
3+
After installation, register `dot-annotated-services` in your project by adding the below line to your configuration aggregator (usually: `config/config.php`):
4+
5+
Dot\AnnotatedServices\ConfigProvider::class,
6+

docs/book/v4/factories.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Factories
2+
3+
`dot-annotated-services` is based on 3 reusable factories - `AnnotatedRepositoryFactory`, `AnnotatedServiceFactory` and `AnnotatedServiceAbstractFactory` - able to inject any dependency into a class.
4+
5+
## AttributedRepositoryFactory
6+
7+
Injects entity repositories into a class.
8+
9+
10+
### Exceptions thrown
11+
- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not exist
12+
- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not extend `Doctrine\ORM\EntityRepository`
13+
- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not have `@Entity` annotation
14+
- `Psr\Container\NotFoundExceptionInterface` if `Doctrine\ORM\EntityManagerInterface` does not exist in the service container
15+
- `Psr\Container\ContainerExceptionInterface` if service manager is unable to provide an instance of `Doctrine\ORM\EntityManagerInterface`
16+
17+
18+
## AttributedServiceFactory
19+
20+
Injects class dependencies into classes.
21+
22+
If a dependency is specified using the dot notation, `AttributedServiceFactory` will try to load a service having that specific alias.
23+
If it does not find one, it will try to load the dependency as a config tree, checking each segment if it's available in the service container.
24+
25+
You can use the inject annotation on setters too, they will be called at creation time and injected with the configured dependencies.
26+
27+
28+
### Exceptions thrown
29+
- `Dot\AnnotatedServices\Exception\RuntimeException` if service does not exist
30+
- `Dot\AnnotatedServices\Exception\RuntimeException` if service does not have `@Inject` annotation on it's constructor
31+
- `ReflectionException` on failure of creating a ReflectionClass of the dependency
32+
- `Psr\Container\NotFoundExceptionInterface` if a dependency does not exist in the service container
33+
- `Psr\Container\ContainerExceptionInterface` if service manager is unable to provide an instance of a dependency
34+
35+
36+
## AnnotatedServiceAbstractFactory
37+
38+
Using this approach, no service manager configuration is required. It uses the registered abstract factory to create annotated services.
39+
40+
In order to tell the abstract factory which services are to be created, you need to annotate the service class with the `@Service` annotation.
41+
42+
```php
43+
<?php
44+
45+
declare(strict_types=1);
46+
47+
namespace YourApp\Service;
48+
49+
/**
50+
* @Dot\AnnotatedServices\Annotation\Service
51+
*/
52+
class Example
53+
{
54+
/**
55+
* @Dot\AnnotatedServices\Annotation\Inject({
56+
* YourApp\Repository\Dependency1::class,
57+
* YourApp\Repository\Dependency2::class,
58+
* "config.example"
59+
* })
60+
*/
61+
public function __construct(
62+
protected YourApp\Repository\Dependency1 $dependency1,
63+
protected YourApp\Helper\Dependency2 $dependency2,
64+
protected array $exampleConfig,
65+
) {
66+
}
67+
}
68+
```
69+
And that's it, you don't need to configure the service manager with this class, creation will happen automatically.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Inject entity repositories
2+
3+
4+
## Prepare repository
5+
6+
`dot-annotated-services` determines the entity a repository is related to by looking at the `@Entity` annotation, added to the repository class.
7+
8+
```php
9+
<?php
10+
11+
declare(strict_types=1);
12+
13+
namespace YourApp\Repository;
14+
15+
/**
16+
* @Dot\AnnotatedServices\Annotation\Entity(name="YourApp\Entity\Example")
17+
*/
18+
class ExampleRepository extends Doctrine\ORM\EntityRepository
19+
{
20+
}
21+
```
22+
23+
Each entity repository must extend `Doctrine\ORM\EntityRepository`.
24+
25+
26+
## Register repository
27+
28+
Open the ConfigProvider of the module where your repository resides.
29+
30+
Add a new entry under `factories`, where the key is your repository FQCN and the value is `Dot\AnnotatedServices\Factory\AnnotatedRepositoryFactory::class`.
31+
32+
See below example for a better understanding of the file structure.
33+
34+
```php
35+
<?php
36+
37+
declare(strict_types=1);
38+
39+
namespace YourApp;
40+
41+
class ConfigProvider
42+
{
43+
public function __invoke(): array
44+
{
45+
return [
46+
'dependencies' => $this->getDependencies(),
47+
];
48+
}
49+
50+
public function getDependencies(): array
51+
{
52+
return [
53+
'factories' => [
54+
YourApp\Repository\ExampleRepository::class => Dot\AnnotatedServices\Factory\AnnotatedRepositoryFactory::class,
55+
],
56+
];
57+
}
58+
}
59+
```

docs/book/v4/factories/service.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Inject class dependencies
2+
3+
4+
## Prepare class
5+
6+
`dot-annotated-services` determines the dependencies by looking at the `@Inject` annotation, added to the constructor of a class.
7+
Dependencies are specified as one parameter, which is an array of FQCNs.
8+
9+
```php
10+
<?php
11+
12+
declare(strict_types=1);
13+
14+
namespace YourApp\Service;
15+
16+
class Example
17+
{
18+
/**
19+
* @Dot\AnnotatedServices\Annotation\Inject({
20+
* YourApp\Repository\Dependency1::class,
21+
* YourApp\Repository\Dependency2::class,
22+
* "config"
23+
* })
24+
*/
25+
public function __construct(
26+
protected YourApp\Repository\Dependency1 $dependency1,
27+
protected YourApp\Helper\Dependency2 $dependency2,
28+
protected array $config
29+
) {
30+
}
31+
}
32+
```
33+
34+
If your class needs the value of a specific configuration key, you can specify the path using dot notation:
35+
36+
```php
37+
/**
38+
* @Dot\AnnotatedServices\Annotation\Inject({
39+
* YourApp\Repository\Dependency1::class,
40+
* YourApp\Repository\Dependency2::class,
41+
* "config.example"
42+
* })
43+
*/
44+
public function __construct(
45+
protected YourApp\Repository\Dependency1 $dependency1,
46+
protected YourApp\Helper\Dependency2 $dependency2,
47+
protected array $exampleConfig,
48+
) {
49+
}
50+
```
51+
52+
53+
## Register class
54+
55+
Open the ConfigProvider of the module where your class resides.
56+
57+
Add a new entry under `factories`, where the key is your class FQCN and the value is `Dot\AnnotatedServices\Factory\AnnotatedServiceFactory::class`.
58+
59+
See below example for a better understanding of the file structure.
60+
61+
```php
62+
<?php
63+
64+
declare(strict_types=1);
65+
66+
namespace YourApp;
67+
68+
class ConfigProvider
69+
{
70+
public function __invoke(): array
71+
{
72+
return [
73+
'dependencies' => $this->getDependencies(),
74+
];
75+
}
76+
77+
public function getDependencies(): array
78+
{
79+
return [
80+
'factories' => [
81+
YourApp\Service\Example::class => Dot\AnnotatedServices\Factory\AnnotatedServiceFactory::class,
82+
],
83+
];
84+
}
85+
}
86+
```

docs/book/v4/installation.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Installation
2+
3+
Install `dotkernel/dot-annotated-services` by executing the following Composer command:
4+
5+
composer require dotkernel/dot-annotated-services

docs/book/v4/overview.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Overview
2+
3+
`dot-annotated-services` is DotKernel's dependency injection service.
4+
5+
By providing reusable factories for service and repository injection, it reduces code complexity in projects.

docs/book/v4/usage.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Usage
2+
3+
Version `4.x` of `dot-annotated-services` is the last version that uses [doctrine/annotations](https://github.com/doctrine/annotations) to parse and inject dependencies.
4+
5+
You can use it to:
6+
- [Inject class dependencies](factories/service.md)
7+
- [Inject entity repositories](factories/repository.md)

mkdocs.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
docs_dir: docs/book
2+
site_dir: docs/html
3+
extra:
4+
project: Packages
5+
current_version: v4
6+
versions:
7+
- v4
8+
nav:
9+
- Home: index.md
10+
- v4:
11+
- Overview: v4/overview.md
12+
- Installation: v4/installation.md
13+
- Usage: v4/usage.md
14+
- Factories: v4/factories.md
15+
- Cache: v4/cache.md
16+
- Reference:
17+
- "Inject class dependencies": v4/factories/service.md
18+
- "Inject entity repositories": v4/factories/repository.md
19+
site_name: dot-annotated-services
20+
site_description: "DotKernel's dependency injection service"
21+
repo_url: "https://github.com/dotkernel/dot-annotated-services"
22+
plugins:
23+
- search
24+
- redirects:
25+
redirect_maps:
26+
overview.md: v4/overview.md
27+
install.md: v4/installation.md
28+
usage.md: v4/usage.md

0 commit comments

Comments
 (0)