diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml
index c8de574..9652006 100644
--- a/.github/workflows/codecov.yml
+++ b/.github/workflows/codecov.yml
@@ -18,6 +18,7 @@ jobs:
- "8.3"
- "8.4"
- "8.5"
+ - "8.6"
steps:
- name: Checkout
@@ -49,6 +50,6 @@ jobs:
run: vendor/bin/phpunit --colors=always --coverage-clover clover.xml
- name: Send code coverage report to Codecov.io
- uses: codecov/codecov-action@v3
+ uses: codecov/codecov-action@v7
with:
token: ${{ secrets.CODECOV_TOKEN }}
diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml
index 9f01888..2dd6164 100644
--- a/.github/workflows/static-analysis.yml
+++ b/.github/workflows/static-analysis.yml
@@ -4,7 +4,7 @@ on:
name: Run PHPStan checks
jobs:
- mutation:
+ static-analysis:
name: PHPStan ${{ matrix.php }}-${{ matrix.os }}
runs-on: ${{ matrix.os }}
@@ -18,6 +18,7 @@ jobs:
- "8.3"
- "8.4"
- "8.5"
+ - "8.6"
steps:
- name: Checkout
@@ -27,7 +28,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
- coverage: pcov
+ coverage: none
ini-values: assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On
tools: composer:v2, cs2pr
@@ -46,4 +47,4 @@ jobs:
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
- name: Run static analysis with PHPStan
- run: vendor/bin/phpstan analyse
+ run: vendor/bin/phpstan analyse --memory-limit 1G
diff --git a/.gitignore b/.gitignore
index 4b3643b..620b02e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,7 @@ composer.phar
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
composer.lock
.phpunit.result.cache
+.phpunit.cache
.phpcs-cache
/documentation-theme/
/docs/html/
diff --git a/.laminas-ci.json b/.laminas-ci.json
index 29216fa..e827682 100644
--- a/.laminas-ci.json
+++ b/.laminas-ci.json
@@ -1,3 +1,6 @@
{
+ "ignore_php_platform_requirements": {
+ "8.6": true
+ },
"backwardCompatibilityCheck": true
}
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 431976b..529de4f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,96 @@
-## 1.0.0 - 2024-06-06
+# Changelog
-Initial tagged release
+All notable changes to this project are documented in this file.
+
+## 2.0.0 - TBD
+
+### Added
+
+- PHP 8.6 support
+- `RuntimeException::notInstantiable()`, `dependencyNotInstantiable()`, `argumentCountMismatch()`, `invalidService()` and `missingPackage()`
+- `InvalidArgumentException::emptySegment()`
+- Documentation for version 2, including an upgrade guide
+
+### Changed
+
+- `doctrine/orm` moved from `require` to `suggest`; applications using `AttributedRepositoryFactory` must require it, and versions below 2.9 or from 4.0 conflict
+- `psr/container` requirement raised to `^1.1 || ^2.0`
+- `AttributedServiceFactory::readKeysFromArray()` takes the full service key as its third parameter
+- PHPUnit 12.5 and PHPStan level 8; the test suite fails on notices, warnings, deprecations and risky tests
+
+### Deprecated
+
+- Nothing.
+
+### Removed
+
+- `AttributedServiceFactory::$originalKey`
+
+### Fixed
+
+- Requesting an abstract class, an enum or a class with a non-public constructor throws `RuntimeException` instead of a PHP `Error`
+- An unregistered dependency that cannot be instantiated without arguments throws `RuntimeException` instead of `ArgumentCountError` or `Error`
+- The number of `#[Inject]` entries is checked against the constructor; extra entries were silently ignored
+- Empty dot-notation segments throw `InvalidArgumentException` instead of reading the `''` key
+- `AttributedRepositoryFactory` checks the type of the entity manager service
+- `AttributedRepositoryFactory` names the missing package when `doctrine/orm` is not installed
+
+## 1.4.1 - 2026-09-16
+
+### Fixed
+
+- FAQ menu entry ([#14](https://github.com/dotkernel/dot-dependency-injection/pull/14))
+
+## 1.4.0 - 2026-09-16
+
+### Changed
+
+- `#[Entity]` targets classes and `#[Inject]` targets methods only ([#13](https://github.com/dotkernel/dot-dependency-injection/pull/13))
+- Documentation expanded with the "Attributes vs. factories" and FAQ pages ([#13](https://github.com/dotkernel/dot-dependency-injection/pull/13))
+
+### Removed
+
+- PHP 8.2 support ([#13](https://github.com/dotkernel/dot-dependency-injection/pull/13))
+
+### Fixed
+
+- A configuration value of `null` was treated as a missing key ([#13](https://github.com/dotkernel/dot-dependency-injection/pull/13))
+- Dot-notation paths continuing past a scalar were silently truncated ([#13](https://github.com/dotkernel/dot-dependency-injection/pull/13))
+- `AttributedRepositoryFactory` returned a repository of the wrong class when the entity did not declare `repositoryClass` ([#13](https://github.com/dotkernel/dot-dependency-injection/pull/13))
+- An `#[Entity]` attribute naming a missing class was not reported ([#13](https://github.com/dotkernel/dot-dependency-injection/pull/13))
+
+## 1.3.0 - 2025-10-30
+
+### Added
+
+- PHP 8.4 and 8.5 support ([#11](https://github.com/dotkernel/dot-dependency-injection/pull/11))
+
+### Removed
+
+- PHP 8.1 support ([#11](https://github.com/dotkernel/dot-dependency-injection/pull/11))
+
+## 1.2.0 - 2025-03-04
+
+### Changed
+
+- Psalm replaced with PHPStan ([#9](https://github.com/dotkernel/dot-dependency-injection/pull/9))
+
+## 1.1.1 - 2025-01-28
+
+### Changed
+
+- Psalm 6.x ([#7](https://github.com/dotkernel/dot-dependency-injection/pull/7))
+
+## 1.1.0 - 2024-10-22
+
+### Added
+
+- PHP 8.4 support ([#5](https://github.com/dotkernel/dot-dependency-injection/pull/5))
+
+### Changed
+
+- `laminas/laminas-coding-standard` 3.x
+
+## 1.0.0 - 2024-06-07
+
+Initial tagged release.
diff --git a/README.md b/README.md
index af218e5..7cee931 100644
--- a/README.md
+++ b/README.md
@@ -5,40 +5,48 @@
Instead of a hand-written factory class per service, you declare a class's dependencies with the `#[Inject]` attribute on its constructor - or a repository's entity with `#[Entity]` - and register one of the two reusable factories this package ships.
That removes an entire category of boilerplate files from a project and keeps the dependency list next to the constructor it feeds.
-See [Attributes vs. factories](https://docs.dotkernel.org/dot-dependency-injection/v1/attributes-vs-factories/) for the full comparison and the trade-offs.
+See [Attributes vs. factories](https://docs.dotkernel.org/dot-dependency-injection/v2/attributes-vs-factories/) for the full comparison and the trade-offs.
## Documentation
Documentation is available at: .
-- [Installation](https://docs.dotkernel.org/dot-dependency-injection/v1/installation/)
-- [Configuration](https://docs.dotkernel.org/dot-dependency-injection/v1/configuration/)
-- [Attributes vs. factories](https://docs.dotkernel.org/dot-dependency-injection/v1/attributes-vs-factories/)
-- [Factories](https://docs.dotkernel.org/dot-dependency-injection/v1/factories/)
-- [Inject class dependencies](https://docs.dotkernel.org/dot-dependency-injection/v1/factories/service/)
-- [Inject entity repositories](https://docs.dotkernel.org/dot-dependency-injection/v1/factories/repository/)
-- [FAQ](https://docs.dotkernel.org/dot-dependency-injection/v1/faq/)
+- [Installation](https://docs.dotkernel.org/dot-dependency-injection/v2/installation/)
+- [Configuration](https://docs.dotkernel.org/dot-dependency-injection/v2/configuration/)
+- [Attributes vs. factories](https://docs.dotkernel.org/dot-dependency-injection/v2/attributes-vs-factories/)
+- [Factories](https://docs.dotkernel.org/dot-dependency-injection/v2/factories/)
+- [Inject class dependencies](https://docs.dotkernel.org/dot-dependency-injection/v2/factories/service/)
+- [Inject entity repositories](https://docs.dotkernel.org/dot-dependency-injection/v2/factories/repository/)
+- [FAQ](https://docs.dotkernel.org/dot-dependency-injection/v2/faq/)
+- [Upgrading from 1.x](https://docs.dotkernel.org/dot-dependency-injection/v2/upgrading/)
+
+## Versions
+
+| Version | Branch | PHP | Status | Documentation |
+| --- | --- | --- | --- | --- |
+| 2.x | `2.0` | 8.3, 8.4, 8.5, 8.6 | active | [v2](https://docs.dotkernel.org/dot-dependency-injection/v2/overview/) |
+| 1.x | `1.0` | 8.3, 8.4, 8.5 | supported | [v1](https://docs.dotkernel.org/dot-dependency-injection/v1/overview/) |
## Badges

-
+
[](https://github.com/dotkernel/dot-dependency-injection/issues)
[](https://github.com/dotkernel/dot-dependency-injection/network)
[](https://github.com/dotkernel/dot-dependency-injection/stargazers)
-[](https://github.com/dotkernel/dot-dependency-injection/blob/1.0/LICENSE.md)
+[](https://github.com/dotkernel/dot-dependency-injection/blob/2.0/LICENSE.md)
-[](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/continuous-integration.yml)
+[](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/continuous-integration.yml)
[](https://codecov.io/gh/dotkernel/dot-dependency-injection)
[](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/docs-build.yml)
-[](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/static-analysis.yml)
+[](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/static-analysis.yml)
## Requirements
-- PHP 8.3, 8.4 or 8.5
-- a PSR-11 container, usually `laminas/laminas-servicemanager`
-- `doctrine/orm` ^2.9 || ^3.0, if you use `AttributedRepositoryFactory`
+- PHP 8.3, 8.4, 8.5 or 8.6
+- a PSR-11 container (`psr/container` ^1.1 || ^2.0), usually `laminas/laminas-servicemanager`
+- `doctrine/orm` ^2.9 || ^3.0, installed by your application, if you use `AttributedRepositoryFactory`
## Installation
@@ -94,7 +102,8 @@ public function __construct(
The `#[Inject]` attribute is telling `AttributedServiceFactory` to inject the services specified as parameters, in the same order as the constructor parameters.
Valid service names should be provided, as registered in the service manager.
-A name that is not registered in the container, but is an existing class, is instantiated directly.
+A name that is not registered in the container, but is a class that can be instantiated without arguments, is instantiated directly.
+The number of entries is checked against the constructor; trailing parameters with a default value may be left out.
A class without a constructor does not need the attribute.
@@ -113,6 +122,12 @@ which will inject `$container->get('config')['debug'];`.
### Using the AttributedRepositoryFactory
+`AttributedRepositoryFactory` needs `doctrine/orm`, which this package does not install:
+
+```shell
+composer require doctrine/orm
+```
+
You can register doctrine repositories and inject them using the `AttributedRepositoryFactory` as below:
```php
diff --git a/SECURITY.md b/SECURITY.md
index e54209b..e1c83a3 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -4,6 +4,7 @@
| Version | Supported | PHP Version |
| --- | --- | --- |
+| 2.x | :white_check_mark: |  |
| 1.x | :white_check_mark: |  |
## Reporting Potential Security Issues
diff --git a/composer.json b/composer.json
index 85c8e44..e106233 100644
--- a/composer.json
+++ b/composer.json
@@ -24,15 +24,21 @@
"service"
],
"require": {
- "php": "~8.3.0 || ~8.4.0 || ~8.5.0",
- "doctrine/orm": "^2.9 || ^3.0",
- "psr/container": "^1.0 || ^2.0"
+ "php": "~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0",
+ "psr/container": "^1.1 || ^2.0"
},
"require-dev": {
+ "doctrine/orm": "^2.9 || ^3.0",
"laminas/laminas-coding-standard": "^3.0",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-phpunit": "^2.0",
- "phpunit/phpunit": "^10.5"
+ "phpunit/phpunit": "^12.5"
+ },
+ "suggest": {
+ "doctrine/orm": "Required to use AttributedRepositoryFactory"
+ },
+ "conflict": {
+ "doctrine/orm": "<2.9 || >=4.0"
},
"autoload": {
"psr-4": {
diff --git a/docs/book/v2/attributes-vs-factories.md b/docs/book/v2/attributes-vs-factories.md
new file mode 100644
index 0000000..740eeed
--- /dev/null
+++ b/docs/book/v2/attributes-vs-factories.md
@@ -0,0 +1,156 @@
+# Attributes vs. factories
+
+A PSR-11 container needs to know how to build every service.
+The usual answer is a factory class per service, which means that a project ends up carrying one extra file for every class it wires up.
+`dot-dependency-injection` replaces those files with a single attribute on the constructor and two reusable factories.
+
+## The same service, both ways
+
+### With a hand-written factory
+
+Two files, plus the registration.
+
+```php
+get(UserRepository::class),
+ $container->get(UrlHelper::class),
+ $container->get('config')['user'],
+ );
+ }
+}
+```
+
+```php
+'factories' => [
+ UserService::class => UserServiceFactory::class,
+],
+```
+
+### With `#[Inject]`
+
+One file, and the registration.
+
+```php
+ [
+ UserService::class => AttributedServiceFactory::class,
+],
+```
+
+## Why this is an advantage
+
+**Fewer files, less code.**
+A module with 20 services no longer needs 20 factory classes.
+The wiring shrinks from a class per service to four lines inside the class that already exists.
+
+**One source of truth.**
+The dependency list sits directly above the constructor it feeds.
+Adding a constructor parameter and forgetting to update the factory is the single most common wiring bug in a factory-based project, and it cannot happen when both live on the same lines.
+
+**No drift, no duplication.**
+Hand-written factories repeat the same `$container->get(...)` pattern hundreds of times across a project.
+Every repetition is a place where a typo, a stale service name, or a copy-paste mistake can hide.
+
+**Nothing to test.**
+A trivial factory is still code, so it shows up in coverage reports and either gets a test that asserts nothing meaningful or drags coverage down.
+The two factories in this package are tested once, here.
+
+**Configuration without boilerplate.**
+`'config.user'` replaces `$container->get('config')['user']` and fails with a clear, package-level exception naming the full path when that key is missing, instead of an `Undefined array key` notice or a silent `null`.
+
+**Repositories for free.**
+`AttributedRepositoryFactory` removes an entire category of factories: every Doctrine repository in a project is otherwise a near-identical factory calling `$container->get(EntityManagerInterface::class)->getRepository(...)`.
+
+**Consistent failures.**
+All wiring errors surface as `Dot\DependencyInjection\Exception\ExceptionInterface`, with messages that name the class, the attribute and the factory involved, rather than whatever each hand-written factory happened to do.
+That includes classes that cannot be instantiated, dependencies that are not registered, and an `#[Inject]` list that does not fit the constructor, which would otherwise end in a PHP `Error` or `ArgumentCountError`.
+
+**Readable classes.**
+Anyone opening the class sees what it needs and where each dependency comes from without opening a second file.
+
+## What you give up
+
+Being explicit about the trade-offs matters more than the line count.
+
+| | Hand-written factory | `#[Inject]` |
+| --- | --- | --- |
+| Wiring is checked by the type system | yes, `new UserService(...)` is analysed | no, names are strings resolved at runtime |
+| Order of arguments verified statically | yes | no, the attribute order must match the constructor; the count is checked at runtime |
+| Reflection at build time | none | one `ReflectionClass` per service creation, not cached |
+| Conditional or computed wiring | anything PHP allows | not supported |
+| Works on third-party classes | yes | only on classes you can annotate |
+
+## When to keep writing a factory
+
+`#[Inject]` covers the common case: a constructor that receives services and configuration.
+Write a factory by hand when you need something else.
+
+- the arguments are computed, conditional, or come from something other than the container
+- the service is decorated, or built through a delegator or an abstract factory
+- you are wiring a class you do not own and cannot annotate
+- the class is intentionally constructed with runtime arguments rather than resolved from the container
+- injection has to happen somewhere other than the constructor
+
+Both styles coexist without any friction: the factory is chosen per service in your `ConfigProvider`, so you can use `AttributedServiceFactory` for the bulk of a module and a hand-written factory for the few services that need one.
diff --git a/docs/book/v2/configuration.md b/docs/book/v2/configuration.md
new file mode 100644
index 0000000..a9e596b
--- /dev/null
+++ b/docs/book/v2/configuration.md
@@ -0,0 +1,19 @@
+# Configuration
+
+After installation, register `dot-dependency-injection` in your project by adding the below line to your configuration aggregator (usually: `config/config.php`):
+
+```php
+Dot\DependencyInjection\ConfigProvider::class,
+```
+
+`Dot\DependencyInjection\ConfigProvider` currently returns an empty array: the package exposes factories that you reference from your own modules, so it does not need to register any service of its own.
+Registering it keeps your configuration aggregate consistent with the other Dotkernel packages and makes sure that future configuration shipped by this package is picked up automatically.
+
+There is no package-specific configuration to set.
+Everything else is declared where it belongs:
+
+- the dependencies of a class, through the `#[Inject]` attribute on its constructor
+- the entity of a repository, through the `#[Entity]` attribute on the repository class
+- the mapping between a class and the factory that builds it, under the `dependencies.factories` key of the `ConfigProvider` of your own module
+
+See [Inject class dependencies](factories/service.md) and [Inject entity repositories](factories/repository.md).
diff --git a/docs/book/v2/factories.md b/docs/book/v2/factories.md
new file mode 100644
index 0000000..7764e1b
--- /dev/null
+++ b/docs/book/v2/factories.md
@@ -0,0 +1,92 @@
+# Factories
+
+`dot-dependency-injection` is based on two reusable factories - `AttributedServiceFactory` and `AttributedRepositoryFactory` - able to inject any dependency into a class.
+
+Both are plain invokable factories, so they work with any PSR-11 container that passes the requested service name to the factory (for example `laminas/laminas-servicemanager`):
+
+```php
+$factory = new Dot\DependencyInjection\Factory\AttributedServiceFactory();
+$service = $factory($container, YourApp\Service\Example::class);
+```
+
+Both expose the same logic through `createObject(ContainerInterface $container, string $requestedName)`, which `__invoke()` simply delegates to.
+Use `createObject()` when you extend one of the factories and need to call the parent implementation explicitly.
+
+> Because the service name is also the class to instantiate, you can only register these factories under the fully qualified class name of the service.
+> Aliases still work, as long as the alias points to a service registered under its FQCN.
+
+## AttributedServiceFactory
+
+Injects class dependencies into classes.
+
+The factory looks for the `#[Inject]` attribute on the constructor of the requested class and resolves each of its parameters, in the declared order, into a constructor argument.
+
+Resolution of a single dependency happens as follows:
+
+1. if the container has a service registered under the exact name, that service is used - this is checked first, even when the name contains dots
+2. otherwise, if the name contains dots, the part before the first dot is resolved from the container and the remaining parts are read as keys from the returned array (or `ArrayAccess` object); an empty part, as in `config.` or `config..debug`, is rejected
+3. otherwise, if the name is a class that can be instantiated without arguments, the factory instantiates it directly with `new $name()`
+4. otherwise, a `RuntimeException` is thrown
+
+Before resolving anything, the factory checks that the number of `#[Inject]` entries fits the constructor: at least the number of required parameters, and at most the total number of parameters unless the last one is variadic.
+
+A class without a constructor does not need the `#[Inject]` attribute: it is instantiated directly.
+
+### Exceptions thrown by AttributedServiceFactory
+
+- `Dot\DependencyInjection\Exception\RuntimeException` if the requested class does not exist
+- `Dot\DependencyInjection\Exception\RuntimeException` if the requested class cannot be instantiated: it is abstract, an enum, or its constructor is not public
+- `Dot\DependencyInjection\Exception\RuntimeException` if the requested class has a constructor without the `#[Inject]` attribute
+- `Dot\DependencyInjection\Exception\RuntimeException` if the class tries to inject itself
+- `Dot\DependencyInjection\Exception\RuntimeException` if the number of `#[Inject]` entries does not fit the constructor
+- `Dot\DependencyInjection\Exception\RuntimeException` if a dependency is neither a registered service nor an existing class
+- `Dot\DependencyInjection\Exception\RuntimeException` if a dependency is an existing class that is not registered and cannot be instantiated without arguments
+- `Dot\DependencyInjection\Exception\InvalidArgumentException` if a key of a dot-separated dependency cannot be found in the array service
+- `Dot\DependencyInjection\Exception\InvalidArgumentException` if a dot-separated dependency contains an empty segment
+- `Psr\Container\NotFoundExceptionInterface` if a dependency does not exist in the service container
+- `Psr\Container\ContainerExceptionInterface` if the service manager is unable to provide an instance of a dependency
+
+## AttributedRepositoryFactory
+
+Injects entity repositories into a class.
+
+It requires `doctrine/orm` ^2.9 or ^3.0, which your application installs: this package only suggests it.
+
+The factory looks for the `#[Entity]` attribute on the requested repository class and returns `$container->get(EntityManagerInterface::class)->getRepository($entityName)`.
+
+Since Doctrine decides which repository class to build from the mapping of the entity, the entity referenced by the `#[Entity]` attribute must declare the repository back:
+
+```php
+#[ORM\Entity(repositoryClass: ExampleRepository::class)]
+```
+
+If it does not, Doctrine returns its default repository and the factory throws a `RuntimeException` instead of handing back an object of an unexpected type.
+
+### Exceptions thrown by AttributedRepositoryFactory
+
+- `Dot\DependencyInjection\Exception\RuntimeException` if `doctrine/orm` is not installed
+- `Dot\DependencyInjection\Exception\RuntimeException` if the repository class does not exist
+- `Dot\DependencyInjection\Exception\RuntimeException` if the repository class does not extend `Doctrine\ORM\EntityRepository`
+- `Dot\DependencyInjection\Exception\RuntimeException` if the repository class does not have the `#[Entity]` attribute
+- `Dot\DependencyInjection\Exception\RuntimeException` if the class referenced by the `#[Entity]` attribute does not exist
+- `Dot\DependencyInjection\Exception\RuntimeException` if the `Doctrine\ORM\EntityManagerInterface` service is not an entity manager
+- `Dot\DependencyInjection\Exception\RuntimeException` if Doctrine returns a repository that is not an instance of the requested class
+- `Psr\Container\NotFoundExceptionInterface` if `Doctrine\ORM\EntityManagerInterface` does not exist in the service container
+- `Psr\Container\ContainerExceptionInterface` if the service manager is unable to provide an instance of `Doctrine\ORM\EntityManagerInterface`
+
+## Exception hierarchy
+
+Both exceptions implement `Dot\DependencyInjection\Exception\ExceptionInterface`, so you can catch anything thrown by this package with a single `catch` block:
+
+```php
+try {
+ $service = $container->get(YourApp\Service\Example::class);
+} catch (Dot\DependencyInjection\Exception\ExceptionInterface $exception) {
+ // ...
+}
+```
+
+| Exception | Extends | Meaning |
+| --- | --- | --- |
+| `RuntimeException` | `\RuntimeException` | the class or service graph cannot be built as declared |
+| `InvalidArgumentException` | `\InvalidArgumentException` | a dot-separated dependency points to a missing array key |
diff --git a/docs/book/v2/factories/repository.md b/docs/book/v2/factories/repository.md
new file mode 100644
index 0000000..7155987
--- /dev/null
+++ b/docs/book/v2/factories/repository.md
@@ -0,0 +1,122 @@
+# Inject entity repositories
+
+`AttributedRepositoryFactory` requires `doctrine/orm` ^2.9 or ^3.0 in your application:
+
+```shell
+composer require doctrine/orm
+```
+
+Without it, the factory throws a `Dot\DependencyInjection\Exception\RuntimeException` that names the missing package.
+
+## Prepare entity
+
+Doctrine decides which repository class to instantiate based on the mapping of the entity, so the entity must point to its repository:
+
+```php
+
+ */
+#[Entity(name: Example::class)]
+class ExampleRepository extends EntityRepository
+{
+}
+```
+
+- the `name` field has to be the fully qualified class name of the entity
+- each entity repository must extend `Doctrine\ORM\EntityRepository`
+- `#[Entity]` targets classes only
+- `#[Entity]` is not inherited: PHP does not pass attributes down to subclasses, so a repository extending another repository needs its own `#[Entity]`
+
+The factory returns `$container->get(EntityManagerInterface::class)->getRepository(Example::class)`, which means the repository is built and managed by Doctrine, with the entity manager already injected.
+Do not declare a constructor with a different signature on your repository.
+
+> If the entity does not declare `repositoryClass`, Doctrine returns its default `EntityRepository` and the factory throws a `Dot\DependencyInjection\Exception\RuntimeException` naming both classes, instead of returning an object of an unexpected type under your repository's service name.
+
+## Register repository
+
+Open the ConfigProvider of the module where your repository resides.
+
+Add a new entry under `factories`, where the key is your repository FQCN and the value is `Dot\DependencyInjection\Factory\AttributedRepositoryFactory::class`.
+
+See the below example for a better understanding of the file structure.
+
+```php
+ $this->getDependencies(),
+ ];
+ }
+
+ public function getDependencies(): array
+ {
+ return [
+ 'factories' => [
+ ExampleRepository::class => AttributedRepositoryFactory::class,
+ ],
+ ];
+ }
+}
+```
+
+`Doctrine\ORM\EntityManagerInterface` must be registered in your container, and the service must be an entity manager; anything else throws a `Dot\DependencyInjection\Exception\RuntimeException` naming the type that was found.
+In Dotkernel projects this is provided by `doctrine/doctrine-orm-module` / `dot-cache` configuration and is already in place.
+
+## Inject the repository into a service
+
+Once registered, the repository is an ordinary service, so it can be injected with `#[Inject]`:
+
+```php
+#[Inject(
+ ExampleRepository::class,
+)]
+public function __construct(
+ protected ExampleRepository $exampleRepository,
+) {
+}
+```
+
+## Caching
+
+The `#[Entity]` attribute is read with reflection on every repository creation and is not cached by this package.
diff --git a/docs/book/v2/factories/service.md b/docs/book/v2/factories/service.md
new file mode 100644
index 0000000..05ef027
--- /dev/null
+++ b/docs/book/v2/factories/service.md
@@ -0,0 +1,139 @@
+# Inject class dependencies
+
+## Prepare class
+
+`dot-dependency-injection` determines the dependencies by looking at the `#[Inject]` attribute, added to the constructor of a class.
+Dependencies are specified as separate parameters of the `#[Inject]` attribute, in the same order as the constructor parameters.
+
+```php
+ `#[Inject]` targets methods only, and only the constructor is inspected.
+> Injecting dependencies into property setters is not supported.
+
+## Inject a configuration value
+
+If your class needs the value of a specific configuration key, you can specify the path using dot notation:
+
+```php
+ #[Inject(
+ Dependency1::class,
+ 'config.example',
+ )]
+ public function __construct(
+ protected Dependency1 $dependency1,
+ protected array $exampleConfig,
+ ) {
+ }
+```
+
+`'config.example'` injects `$container->get('config')['example']`, and paths of any depth work: `'config.example.nested.value'`.
+
+Things worth knowing about dot notation:
+
+- the full name is always looked up in the container first, so a service literally registered as `config.example` takes precedence over the `example` key of the `config` service
+- only the part before the first dot is resolved from the container; every following segment is an array key
+- both arrays and objects implementing `ArrayAccess` can be traversed
+- a key holding `null` is injected as `null`, it is not treated as missing
+- an empty segment, as in `config.`, `.config` or `config..debug`, throws `Dot\DependencyInjection\Exception\InvalidArgumentException`, unless a service is registered under that exact name
+- if a segment does not exist, or the path continues past a scalar value, `Dot\DependencyInjection\Exception\InvalidArgumentException` is thrown, naming the full path you declared
+
+## Register class
+
+Open the ConfigProvider of the module where your class resides.
+
+Add a new entry under `factories`, where the key is your class FQCN and the value is `Dot\DependencyInjection\Factory\AttributedServiceFactory::class`.
+
+See the below example for a better understanding of the file structure.
+
+```php
+ $this->getDependencies(),
+ ];
+ }
+
+ public function getDependencies(): array
+ {
+ return [
+ 'factories' => [
+ Example::class => AttributedServiceFactory::class,
+ ],
+ ];
+ }
+}
+```
+
+> The service key must be the fully qualified class name of the class to build, because the factory instantiates the requested service name.
+> Register an alias if you need a shorter name.
+
+## Recursion
+
+A class cannot inject itself:
+
+```php
+#[Inject(self::class)]
+public function __construct(protected ?Example $example = null)
+{
+}
+```
+
+This throws a `Dot\DependencyInjection\Exception\RuntimeException`.
+
+> Only direct self-injection is detected.
+> A cycle spanning several classes (`A` needs `B`, `B` needs `A`) is not detected by this package and will end in infinite recursion, exactly as it would with hand-written factories.
+
+## Caching
+
+Dependencies declared through `#[Inject]` are resolved with reflection on every service creation, and the result is not cached by this package.
+The container's own instance cache still applies: a shared service is built once per request, no matter how many classes inject it.
diff --git a/docs/book/v2/faq.md b/docs/book/v2/faq.md
new file mode 100644
index 0000000..a7359f2
--- /dev/null
+++ b/docs/book/v2/faq.md
@@ -0,0 +1,176 @@
+# FAQ
+
+## Setup and registration
+
+### Do I still have to register every service in a ConfigProvider?
+
+Yes.
+This package replaces the factory class you would write for a service, not the `dependencies.factories` entry that tells the container which factory to use.
+Each service still needs one line mapping its FQCN to `AttributedServiceFactory` or `AttributedRepositoryFactory`.
+
+### Why does it not read the dependencies from my constructor type hints?
+
+Container identifiers are not types.
+The same type can be registered several times under different names, plenty of services are registered under strings such as `config`, and a parameter typed against an interface gives no indication of which implementation you want.
+`#[Inject]` asks you for the identifier because that is the only thing the container can actually resolve.
+
+### Can I register a service under a name other than its fully qualified class name?
+
+Not directly, because the factory instantiates the requested service name.
+Register the service under its FQCN and add an alias pointing to it if you need a shorter name.
+
+### Can I keep some hand-written factories?
+
+Yes, the factory is chosen per service, so both styles can live side by side in the same module.
+See [Attributes vs. factories](attributes-vs-factories.md) for the cases where a hand-written factory is still the better choice.
+
+## The `#[Inject]` attribute
+
+### My constructor takes no arguments, do I need the attribute?
+
+It depends on whether the constructor exists at all.
+A class with no constructor is instantiated directly and needs no attribute.
+A class that explicitly declares `public function __construct()` does need the attribute, otherwise you get `You need to use the "...\Inject" attribute on the "..." class`.
+Use `#[Inject()]` with no parameters, or delete the empty constructor.
+
+### What happens if the order in the attribute does not match the constructor?
+
+The order is not checked, only the number of entries is, and the arguments are passed positionally in the order you listed them.
+If the types differ you get a `TypeError` when the service is created; if they happen to be compatible, the wrong values are injected silently.
+Keep the attribute parameters in the same order as the constructor parameters.
+
+### Can I leave out some constructor parameters?
+
+Only trailing ones that have a default value.
+Arguments are passed positionally, so you cannot skip a parameter in the middle of the list and you cannot inject by parameter name.
+
+### What if I list more or fewer dependencies than the constructor takes?
+
+The factory throws a `Dot\DependencyInjection\Exception\RuntimeException` before resolving anything, naming the class, the number of entries and the number of parameters the constructor accepts.
+A variadic last parameter accepts any number of extra entries.
+
+### Why do I get "is not registered in the container and can not be instantiated without arguments"?
+
+A dependency named in `#[Inject]` is an existing class, but it is not registered in the container and the factory cannot build it with `new` alone: its constructor has required parameters, it is abstract or an enum, or its constructor is not public.
+Register the dependency in your container, for example with `AttributedServiceFactory` and its own `#[Inject]`.
+
+### Can I inject a plain value, such as a string or a number?
+
+No.
+Every parameter of the attribute is resolved as a container identifier, a class name, or a dot-separated path into an array service.
+Put the literal in your configuration and inject it with dot notation instead.
+
+### Is `#[Inject]` inherited by subclasses?
+
+Yes.
+If a subclass does not declare its own constructor, the factory reads the attribute from the inherited constructor and builds the subclass with the dependencies the parent declared.
+Declare a constructor with its own `#[Inject]` in the subclass to change them.
+
+### Can I put `#[Inject]` on a property or a setter?
+
+No.
+The attribute targets methods, and only the constructor is inspected.
+
+### Can I inject the same service twice?
+
+Yes, list it as many times as your constructor needs it.
+A shared service is resolved from the container each time, so both parameters receive the same instance.
+
+## Configuration values
+
+### How do I inject a single configuration key?
+
+Use dot notation: `#[Inject('config.example')]` injects `$container->get('config')['example']`.
+Paths of any depth work, and both arrays and `ArrayAccess` objects can be traversed.
+
+### What if a service is registered under a name that contains dots?
+
+The full name is always looked up in the container first, so a service literally registered as `config.example` wins over the `example` key of the `config` service.
+
+### What if the key has a stray dot, such as `config.` or `config..debug`?
+
+`Dot\DependencyInjection\Exception\InvalidArgumentException` is thrown, because an empty segment is almost always a typo.
+A service registered under that exact name still wins, since the full name is looked up first.
+
+### Can I address a configuration key that itself contains a dot?
+
+No.
+Only the segment before the first dot is treated as a service identifier; every following dot starts a new array key, so a key named `my.key` cannot be reached.
+Nest the value one level deeper, or expose it as its own service.
+
+### What if the configuration value is `null`?
+
+It is injected as `null`.
+A key that exists but holds `null` is not treated as missing.
+
+### What if the key does not exist?
+
+`Dot\DependencyInjection\Exception\InvalidArgumentException` is thrown, naming the full path you declared.
+You get the same exception when the path continues past a scalar value, for example `config.debug.verbose` where `config.debug` is a boolean.
+
+## Doctrine repositories
+
+### Why do I get a repository of the wrong class, or an "unexpected repository" error?
+
+Doctrine, not this package, decides which class to instantiate, and it reads that from the mapping of the entity.
+Add `#[ORM\Entity(repositoryClass: ExampleRepository::class)]` to the entity, so that it points back to the repository that declares it through `#[Entity]`.
+
+### Do I still need `#[Entity]` if the entity already declares `repositoryClass`?
+
+Yes.
+The two point at each other: `repositoryClass` tells Doctrine which class to build, and `#[Entity]` tells the factory which entity to ask for.
+
+### Can I inject extra dependencies into a repository?
+
+No.
+The repository is constructed by Doctrine with the entity manager and the class metadata, so its constructor signature is not yours to change.
+Put the extra dependencies in a service that receives the repository through `#[Inject]`.
+
+### How do I inject a repository into a service?
+
+Register the repository with `AttributedRepositoryFactory`, then list it like any other service: `#[Inject(ExampleRepository::class)]`.
+
+### Do I need `doctrine/orm` if I only use `AttributedServiceFactory`?
+
+No.
+Install it yourself (`composer require doctrine/orm`) only if you use `AttributedRepositoryFactory`.
+
+### Is `#[Entity]` inherited by a subclass of my repository?
+
+No.
+PHP does not pass attributes down to subclasses, so each repository registered with `AttributedRepositoryFactory` needs its own `#[Entity]`.
+
+## Performance
+
+### Is the attribute lookup cached?
+
+No.
+One `ReflectionClass` is created per service creation and the attribute is read every time.
+The container's own instance cache still applies, so a shared service is built once per request no matter how many classes inject it.
+
+### Does that slow down my application?
+
+The reflection happens once per service that a request actually builds, and it is a lookup on an already-loaded class rather than file parsing.
+If a profile ever shows it mattering for a specific hot service, write a factory by hand for that one service.
+
+## Testing and debugging
+
+### How do I unit test a class that uses `#[Inject]`?
+
+Exactly as you would test any other class: instantiate it with `new` and pass test doubles.
+The attribute is inert outside the factory, so it has no effect on your tests.
+
+### I get "You need to use the ... attribute on the ... class", what now?
+
+The requested class has a constructor without the `#[Inject]` attribute, or the attribute sits somewhere other than the constructor.
+Check that the attribute is imported from `Dot\DependencyInjection\Attribute\Inject`, because an attribute of another `Inject` class is ignored.
+
+### Are circular dependencies detected?
+
+Only direct self-injection, which throws a `RuntimeException`.
+A cycle spanning several classes, where `A` needs `B` and `B` needs `A`, is not detected and ends in infinite recursion, exactly as it would with hand-written factories.
+
+### Which exception should I catch?
+
+Both exceptions of this package implement `Dot\DependencyInjection\Exception\ExceptionInterface`, so a single `catch` covers every wiring error.
+Note that your container may wrap them; for example, Laminas Service Manager wraps them in `Laminas\ServiceManager\Exception\ServiceNotCreatedException`, so inspect the previous exception.
diff --git a/docs/book/v2/installation.md b/docs/book/v2/installation.md
new file mode 100644
index 0000000..3cce363
--- /dev/null
+++ b/docs/book/v2/installation.md
@@ -0,0 +1,24 @@
+# Installation
+
+## Requirements
+
+- PHP 8.3, 8.4, 8.5 or 8.6
+- A PSR-11 container (`psr/container` ^1.1 || ^2.0), usually `laminas/laminas-servicemanager`
+- `doctrine/orm` ^2.9 || ^3.0, installed by your application, only if you use `AttributedRepositoryFactory`
+
+## Install
+
+Install `dotkernel/dot-dependency-injection` by executing the following Composer command:
+
+```shell
+composer require dotkernel/dot-dependency-injection
+```
+
+`doctrine/orm` is not installed with this package.
+If you use `AttributedRepositoryFactory`, require it in your application:
+
+```shell
+composer require doctrine/orm
+```
+
+Upgrading from 1.x? See [Upgrading from 1.x](upgrading.md).
diff --git a/docs/book/v2/overview.md b/docs/book/v2/overview.md
new file mode 100644
index 0000000..241b058
--- /dev/null
+++ b/docs/book/v2/overview.md
@@ -0,0 +1,30 @@
+# Overview
+
+`dot-dependency-injection` is Dotkernel's dependency injection service.
+
+Instead of a handwritten factory class per service, you declare a class's dependencies with the `#[Inject]` attribute on its constructor - or a repository's entity with `#[Entity]` - and register one of the two reusable factories this package ships.
+That removes an entire category of boilerplate files from a project and keeps the dependency list next to the constructor it feeds.
+
+It provides:
+
+- `Dot\DependencyInjection\Attribute\Inject` - declares the dependencies of a constructor
+- `Dot\DependencyInjection\Attribute\Entity` - declares the entity of a Doctrine repository
+- `Dot\DependencyInjection\Factory\AttributedServiceFactory` - builds any class from its `#[Inject]` attribute
+- `Dot\DependencyInjection\Factory\AttributedRepositoryFactory` - builds any Doctrine repository from its `#[Entity]` attribute
+
+Continue with [Attributes vs. factories](attributes-vs-factories.md) for the comparison and the trade-offs, or with [Installation](installation.md).
+
+## Badges
+
+
+
+
+[](https://github.com/dotkernel/dot-dependency-injection/issues)
+[](https://github.com/dotkernel/dot-dependency-injection/network)
+[](https://github.com/dotkernel/dot-dependency-injection/stargazers)
+[](https://github.com/dotkernel/dot-dependency-injection/blob/2.0/LICENSE.md)
+
+[](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/continuous-integration.yml)
+[](https://codecov.io/gh/dotkernel/dot-dependency-injection)
+[](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/docs-build.yml)
+[](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/static-analysis.yml)
diff --git a/docs/book/v2/upgrading.md b/docs/book/v2/upgrading.md
new file mode 100644
index 0000000..ebf417f
--- /dev/null
+++ b/docs/book/v2/upgrading.md
@@ -0,0 +1,70 @@
+# Upgrading from 1.x
+
+Version 2.0 keeps the attributes, the factories and the way you register them.
+Most applications upgrade by changing the version constraint and, if they use `AttributedRepositoryFactory`, requiring `doctrine/orm` themselves.
+
+```shell
+composer require dotkernel/dot-dependency-injection:^2.0
+```
+
+## Requirements
+
+| | 1.x | 2.0 |
+| --- | --- | --- |
+| PHP | 8.3, 8.4, 8.5 | 8.3, 8.4, 8.5, 8.6 |
+| `psr/container` | ^1.0 \|\| ^2.0 | ^1.1 \|\| ^2.0 |
+| `doctrine/orm` | ^2.9 \|\| ^3.0, installed by this package | ^2.9 \|\| ^3.0, installed by your application when you use `AttributedRepositoryFactory` |
+
+## Changes that can break your code
+
+| Change | Symptom | Fix |
+| --- | --- | --- |
+| `doctrine/orm` is no longer installed by this package | `"Dot\DependencyInjection\Factory\AttributedRepositoryFactory" requires "doctrine/orm"`, or `Class "Doctrine\ORM\..." not found` in your own code | `composer require doctrine/orm` |
+| `doctrine/orm` 4 is blocked by a `conflict` rule | Composer refuses to resolve | stay on `doctrine/orm` ^2.9 or ^3.0 |
+| `psr/container` 1.0 is no longer supported | Composer refuses to resolve | update `psr/container` to 1.1 or 2.x |
+| `AttributedServiceFactory::$originalKey` was removed | `Undefined property` in a subclass of the factory | use the `$serviceKey` parameter of `readKeysFromArray()` |
+| `readKeysFromArray()` has a third parameter, `string $serviceKey` | fatal error in a subclass that overrides it: declaration must be compatible | add the parameter to your override |
+| an abstract class, an enum or a class with a non-public constructor is requested | `RuntimeException` instead of PHP `Error` | register a concrete class |
+| an unregistered dependency needs constructor arguments, or is abstract or an enum | `RuntimeException` instead of `ArgumentCountError` or `Error` | register the dependency in your container |
+| the number of `#[Inject]` entries is checked against the constructor | `RuntimeException` for missing or extra entries; extra entries used to be ignored silently | make the list match the constructor |
+| empty dot-notation segments are rejected | `InvalidArgumentException` for keys such as `config.` or `config..debug` | remove the stray dot |
+| the `Doctrine\ORM\EntityManagerInterface` service is type-checked | `RuntimeException` instead of PHP `Error` when the service is not an entity manager | register an entity manager under that key |
+
+All new exceptions implement `Dot\DependencyInjection\Exception\ExceptionInterface`, so code that catches it keeps working.
+
+## Overriding `readKeysFromArray()`
+
+Before:
+
+```php
+use Dot\DependencyInjection\Factory\AttributedServiceFactory;
+
+class MyFactory extends AttributedServiceFactory
+{
+ protected function readKeysFromArray(array $keys, mixed $array): mixed
+ {
+ // $this->originalKey held the full dotted key
+ return parent::readKeysFromArray($keys, $array);
+ }
+}
+```
+
+After:
+
+```php
+use Dot\DependencyInjection\Factory\AttributedServiceFactory;
+
+class MyFactory extends AttributedServiceFactory
+{
+ protected function readKeysFromArray(array $keys, mixed $array, string $serviceKey): mixed
+ {
+ // $serviceKey holds the full dotted key
+ return parent::readKeysFromArray($keys, $array, $serviceKey);
+ }
+}
+```
+
+## Checking an application
+
+After upgrading, build every service once, for example with a test that requests each key of your `dependencies.factories` configuration from the container.
+The new checks run when a service is built, so a mismatch that 1.x ignored shows up there with a message naming the class.
diff --git a/docs/book/v2/usage.md b/docs/book/v2/usage.md
new file mode 100644
index 0000000..535ea00
--- /dev/null
+++ b/docs/book/v2/usage.md
@@ -0,0 +1,14 @@
+# Usage
+
+Start with [Attributes vs. factories](attributes-vs-factories.md) for what this package replaces and why.
+
+You can use it to:
+
+- [Inject class dependencies](factories/service.md)
+- [Inject entity repositories](factories/repository.md)
+
+Both are described in detail in [Factories](factories.md).
+
+For the questions that come up most often, see the [FAQ](faq.md).
+
+Coming from 1.x? See [Upgrading from 1.x](upgrading.md).
diff --git a/mkdocs.yml b/mkdocs.yml
index 2123c6f..5f9c893 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -2,11 +2,24 @@ docs_dir: docs/book
site_dir: docs/html
extra:
project: Packages
- current_version: v1
+ current_version: v2
versions:
+ - v2
- v1
nav:
- Home: index.md
+ - v2:
+ - Overview: v2/overview.md
+ - Installation: v2/installation.md
+ - Configuration: v2/configuration.md
+ - Usage: v2/usage.md
+ - FAQ: v2/faq.md
+ - "Attributes vs. factories": v2/attributes-vs-factories.md
+ - Factories: v2/factories.md
+ - Reference:
+ - "Inject class dependencies": v2/factories/service.md
+ - "Inject entity repositories": v2/factories/repository.md
+ - "Upgrading from 1.x": v2/upgrading.md
- v1:
- Overview: v1/overview.md
- Installation: v1/installation.md
diff --git a/phpstan.neon b/phpstan.neon
index 349be25..08d6acd 100644
--- a/phpstan.neon
+++ b/phpstan.neon
@@ -1,7 +1,7 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
- level: 5
+ level: 8
paths:
- src
- test
diff --git a/phpunit.xml b/phpunit.xml
index 2f742e5..5243a45 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -1,13 +1,24 @@
-
+./test./test/TestData
-
-
+ ./src
diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php
index 03452bb..5c8fd4e 100644
--- a/src/ConfigProvider.php
+++ b/src/ConfigProvider.php
@@ -6,6 +6,9 @@
class ConfigProvider
{
+ /**
+ * @return array
+ */
public function __invoke(): array
{
return [];
diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php
index 026ee4b..e38a8df 100644
--- a/src/Exception/InvalidArgumentException.php
+++ b/src/Exception/InvalidArgumentException.php
@@ -8,11 +8,18 @@
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
- public const MESSAGE_MISSING_KEY =
+ public const MESSAGE_EMPTY_SEGMENT =
+ 'The dotted key "%s" contains an empty segment.';
+ public const MESSAGE_MISSING_KEY =
'The key "%s" provided in the dotted notation could not be found in the array service.';
public static function missingKey(string $serviceKey): self
{
return new self(sprintf(self::MESSAGE_MISSING_KEY, $serviceKey));
}
+
+ public static function emptySegment(string $serviceKey): self
+ {
+ return new self(sprintf(self::MESSAGE_EMPTY_SEGMENT, $serviceKey));
+ }
}
diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php
index 14c73b9..8a85608 100644
--- a/src/Exception/RuntimeException.php
+++ b/src/Exception/RuntimeException.php
@@ -8,15 +8,25 @@
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
- public const MESSAGE_ATTRIBUTE_NOT_FOUND =
+ public const MESSAGE_ARGUMENT_COUNT =
+ 'The #[Inject] attribute of "%s" lists %d dependencies, but its constructor accepts %d to %d.';
+ public const MESSAGE_ATTRIBUTE_NOT_FOUND =
'You need to use the "%s" attribute on the "%s" class so that "%s" can create it.';
- public const MESSAGE_CLASS_NOT_FOUND =
+ public const MESSAGE_CLASS_NOT_FOUND =
'Defined injectable "%s" could not be found in container or as a class.';
- public const MESSAGE_DOES_NOT_EXTEND =
+ public const MESSAGE_DEPENDENCY_NOT_INSTANTIABLE =
+ 'Injectable "%s" is not registered in the container and can not be instantiated without arguments.';
+ public const MESSAGE_DOES_NOT_EXTEND =
'Class "%s" must extend class "%s".';
- public const MESSAGE_RECURSIVE_INJECT =
+ public const MESSAGE_INVALID_SERVICE =
+ 'Service "%s" must be an instance of "%s", "%s" given.';
+ public const MESSAGE_MISSING_PACKAGE =
+ '"%2$s" requires "%1$s": run "composer require %1$s".';
+ public const MESSAGE_NOT_INSTANTIABLE =
+ 'Class "%s" can not be instantiated: it is abstract, an enum, or its constructor is not public.';
+ public const MESSAGE_RECURSIVE_INJECT =
'Class "%s" can not be injected into itself.';
- public const MESSAGE_UNEXPECTED_REPOSITORY =
+ public const MESSAGE_UNEXPECTED_REPOSITORY =
'Doctrine returned an instance of "%s" instead of "%s". '
. 'Make sure that entity "%s" declares #[ORM\Entity(repositoryClass: ...)] pointing to it.';
@@ -44,4 +54,29 @@ public static function unexpectedRepository(string $actual, string $requestedNam
{
return new self(sprintf(self::MESSAGE_UNEXPECTED_REPOSITORY, $actual, $requestedName, $entity));
}
+
+ public static function invalidService(string $serviceName, string $class, string $actual): self
+ {
+ return new self(sprintf(self::MESSAGE_INVALID_SERVICE, $serviceName, $class, $actual));
+ }
+
+ public static function missingPackage(string $package, string $class): self
+ {
+ return new self(sprintf(self::MESSAGE_MISSING_PACKAGE, $package, $class));
+ }
+
+ public static function notInstantiable(string $requestedName): self
+ {
+ return new self(sprintf(self::MESSAGE_NOT_INSTANTIABLE, $requestedName));
+ }
+
+ public static function dependencyNotInstantiable(string $dependency): self
+ {
+ return new self(sprintf(self::MESSAGE_DEPENDENCY_NOT_INSTANTIABLE, $dependency));
+ }
+
+ public static function argumentCountMismatch(string $requestedName, int $count, int $min, int $max): self
+ {
+ return new self(sprintf(self::MESSAGE_ARGUMENT_COUNT, $requestedName, $count, $min, $max));
+ }
}
diff --git a/src/Factory/AttributedRepositoryFactory.php b/src/Factory/AttributedRepositoryFactory.php
index dc38488..14e82fc 100644
--- a/src/Factory/AttributedRepositoryFactory.php
+++ b/src/Factory/AttributedRepositoryFactory.php
@@ -14,6 +14,7 @@
use ReflectionClass;
use function class_exists;
+use function get_debug_type;
use function is_a;
/**
@@ -22,6 +23,7 @@
class AttributedRepositoryFactory
{
/**
+ * @return EntityRepository