https://github.com/yiisoft/factory
Object factory that is able to resolve dependencies from PSR-11 container
https://github.com/yiisoft/factory
factory hacktoberfest yii3
Last synced: 2 months ago
JSON representation
Object factory that is able to resolve dependencies from PSR-11 container
- Host: GitHub
- URL: https://github.com/yiisoft/factory
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2019-06-10T08:48:15.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-31T13:31:09.000Z (10 months ago)
- Last Synced: 2024-10-29T14:35:36.381Z (7 months ago)
- Topics: factory, hacktoberfest, yii3
- Language: PHP
- Homepage: https://www.yiiframework.com/
- Size: 339 KB
- Stars: 34
- Watchers: 19
- Forks: 14
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Yii Factory
[](https://packagist.org/packages/yiisoft/factory)
[](https://packagist.org/packages/yiisoft/factory)
[](https://github.com/yiisoft/factory/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/factory)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/factory/master)
[](https://github.com/yiisoft/factory/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/yiisoft/factory)This package provides abstract object factory allowing to create objects by given definition
with dependencies resolved by a [PSR-11](https://www.php-fig.org/psr/psr-11/) container.## Requirements
- PHP 8.0 or higher.
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
composer require yiisoft/factory
```## General usage
The factory is useful if you need to create objects using [definition syntax](https://github.com/yiisoft/definitions)
and/or want to configure defaults for objects created.```php
$container = new PSR11DependencyInjectionContainer();
$factoryConfig = [
EngineInterface::class => [
'class' => EngineMarkOne::class,
'__construct()' => [
'power' => 42,
],
]
];$factory = new Factory($container, $factoryConfig);
$one = $factory->create(EngineInterface::class);
$two = $factory->create([
'class' => EngineInterface::class,
'__construct()' => [
'power' => 146,
],
]);
```In the code above we define factory config specifying that when we need `EngineInterface`, an instance of `EngineMarkOne`
will be created with `power` constructor argument equals to 42. We also specify that all the dependencies requested by
the object created should be resolved by `PSR11DependencyInjectionContainer`.First call to `create()` uses default configuration of `EngineInterface` as is. Second call specifies custom
configuration for `power` constructor argument. In this case, configuration specified is merged with default
configuration overriding its keys when the key name is the same.### Tuning for production
By default, the factory validates definitions right when they are set. In production environment, it makes sense to
turn it off by passing `false` as a third constructor argument:```php
$factory = new Factory($container, $factoryConfig, false);
```### Strict factory
`StrictFactory` differs in that it processes only configured definitions.
When attempting to request an existing class that is not defined in the factory config,
a `NotFoundException` will be thrown.```php
$container = new PSR11DependencyInjectionContainer();
$factoryConfig = [
EngineInterface::class => [
'class' => EngineMarkOne::class,
'__construct()' => [
'power' => 42,
],
]
];$factory = new Factory($factoryConfig, $container);
$engine = $factory->create(EngineInterface::class);
// Throws `NotFoundException`
$factory->create(EngineMarkOne::class);
```## Documentation
- [Internals](docs/internals.md)
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).## License
The Yii Factory is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)