Download the PHP package tomphp/config-service-provider without Composer
On this page you can find all versions of the php package tomphp/config-service-provider. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tomphp/config-service-provider
More information about tomphp/config-service-provider
Files in tomphp/config-service-provider
Package config-service-provider
Short Description Configure your application and the Dependency Injection Container (DIC) via config arrays or config files.
License MIT
Homepage https://github.com/tomphp/config-service-provider
Informations about the package config-service-provider
Container Configurator
This package enables you to configure your application and the Dependency Injection Container (DIC) via config arrays or files. Currently, supported containers are:
Installation
Installation can be done easily using composer:
Example Usage
Reading Files From Disk
Instead of providing the config as an array, you can also provide a list of
file pattern matches to the fromFiles
function.
configFromFile(string $filename)
reads config in from a single file.
configFromFiles(string $pattern)
reads config from multiple files using
globbing patterns.
Merging
The reader matches files in the order they are specified. As files are read their config is merged in; overwriting any matching keys.
Supported Formats
Currently .php
and .json
files are supported out of the box. PHP
config files must return a PHP array.
.yaml
and .yml
files can be read when the package symfony/yaml
is
available. Run
to install it.
Application Configuration
All values in the config array are made accessible via the DIC with the keys
separated by a separator (default: .
) and prefixed with constant string (default:
config
).
Example
Accessing A Whole Sub-Array
Whole sub-arrays are also made available for cases where you want them instead of individual values.
Example
Configuring Services
Another feature is the ability to add services to your container via the
config. By default, this is done by adding a services
key under a di
key in
the config in the following format:
Service Aliases
You can create an alias to another service by using the service
keyword
instead of class
:
Service Factories
If you require some addition additional logic when creating a service, you can define a Service Factory. A service factory is simply an invokable class which can take a list of arguments and returns the service instance.
Services are added to the container by using the factory
key instead of the
class
key.
Example Config
Example Service Factory
Injecting The Container
In the rare case that you want to inject the container in as a dependency to
one of your services, you can use Configurator::container()
as the name
of the injected dependency. This will only work in PHP config files, it's not
available with YAML or JSON.
Configuring Inflectors
It is also possible to set up
Inflectors by adding an
inflectors
key to the di
section of the config.
Extra Settings
The behaviour of the Configurator
can be adjusted by using the
withSetting(string $name, $value
method:
Available settings are:
Name | Description | Default |
---|---|---|
SETTING_PREFIX | Sets prefix name for config value keys. | config |
SETTING_SEPARATOR | Sets the separator for config key. | . |
SETTING_SERVICES_KEY | Where the config for the services is. | di.services |
SETTING_INFLECTORS_KEY | Where the config for the inflectors is. | di.inflectors |
SETTING_DEFAULT_SINGLETON_SERVICES | Sets whether services are singleton by default. | false |
Advanced Customisation
Adding A Custom File Reader
You can create your own custom file reader by implementing the
TomPHP\ContainerConfigurator\FileReader\FileReader
interface. Once you have
created it, you can use the
withFileReader(string $extension, string $readerClassName)
method to enable
the it.
IMPORTANT: withFileReader()
must be called before calling
configFromFile()
or configFromFiles()
!
Adding A Custom Container Adapter
You can create your own container adapter so that you can configure other
containers. This is done by implementing the
TomPHP\ContainerConfigurator\FileReader\ContainerAdapter
interface. Once you
have created your adapter, you can use the
withContainerAdapter(string $containerName, string $adapterName)
method to
enable the it:
All versions of config-service-provider with dependencies
beberlei/assert Version ^2.6
tomphp/exception-constructor-tools Version ^1.0.0