Download the PHP package renakdup/simple-dic without Composer
On this page you can find all versions of the php package renakdup/simple-dic. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download renakdup/simple-dic
More information about renakdup/simple-dic
Files in renakdup/simple-dic
Package simple-dic
Short Description Simple DI Container with autowiring for your WordPress application with NO dependencies.
License MIT
Informations about the package simple-dic
Simple DIC - PHP DI Container in one file for your WordPress.
Simple DI Container with autowiring in a single file with NO dependencies allows you to easily use it in your PHP applications and especially convenient for WordPress plugins and themes.
Why choose Simple DI Container
- Easy to integrate in your WordPress project - just copy one file or install via Composer.
- No dependencies on other scripts or libraries.
- Supports auto-wiring
__constructor
parameters for classes as well as for scalar types that have default values. - Supports PHP ^7.4|^8.
- Allow you following the best practices for developing your code.
- No PHPCS conflicts.
- Supports Lazy Load class instantiating.
- Lightweight and fast.
How to install in a project
There are several ways, you can choose what you more prefer:
Install via Composer
Copy and paste file
- Just copy the file
./src/Container.php
to your plugin directory or theme. - Rename
namespace
in the file
fromRenakdup\SimpleDIC
to<Your_Plugin_Name>\SimpleDIC
- Include the file.
How to use it
Get started:
- Create container instance.
- Set a service
- Get a service
- Use object
SimpleDIC allows to set values for the container for primitive types:
Method get()
can resolve not set object in the $container
and then save resolved results in the $container
. It means when you run $container->get( $service )
several times you get the same object.
If you want to instantiate service several time use make()
method.
Factory
Factory is an anonymous function
that wrap creating an instance.
Allows to configure how an object will be created and allows to use Conainer
instance inside the factory.
As well factories create objects in the Lazy Load mode. It means that object will be created just when you resolve it by using get()
method:
Container inside factory
SimpleDIC allows to get a Container
instance inside a factory if you add parameter in a callback ( Container $c )
. This allows to get or resolve another services inside for building an object:
Autowiring
SimpleDIС autowiring feature allows to Container
automatically create and inject dependencies.
I'll show an example:
And then when you create Paypal::class
, you run $container->get(Paypal::class)
, and Container
identifies all classes in the constructor and resolves them. As if it's:
Container autowiring can resolve default values for primitive parameters in a constructor:
You can use auto-wiring feature that allows to Container
create an instances that requires in the __constructor
of class as well as it resolves constructor dependencies for
[!NOTE] But if object creating is more complex and requires configuring and you don't have parameters with default values in the constructor then you need to use
factory
for preparing service.
Create an instance every time
Method make()
resolves services by its name. It returns a new instance of service every time and supports auto-wiring.
[!NOTE]
Constructor's dependencies will not instantiate every time.
If dependencies were resolved before then they will be passed as resolved dependencies.
Consider example:
Dependencies of PayPal service will not be recreated and will be taken from already resolved objects.
PSR11 Compatibility
in progress
Roadmap
- [x] Add binding services with configuration
- [x] Add auto-wiring for registered classes in DIC
- [x] Add auto-wiring for defaults primitives for auto-fillings
- [x] Add supporting invocable classes
- [x] Add PSR11 interfaces in the Container.php.
- [x] Add auto-wiring support for not bounded classes.
- [x] Add resolved service storage (getting singleton).
- [x] Add ability to create new instances of service every time.
- [x] Improve performance.
- [x] Fix deprecated
Use ReflectionParameter::getType() and the ReflectionType APIs should be used instead.
for PHP8 - [x] Bind $container instance by default.
- [ ] Add
remove
method. - [ ] Circular dependency protector.
Nice to have
- [x] Integrate CI with running autotests
- [x] Add badges with tests passed
- [x] Add PHPStan analyzer
- [ ] Add code coverage badge
- [ ] Add descriptions in the code for functions.
- [ ] Choose codestyle
- [ ] Add on packegist
- [ ] Add if class exist checks in the Container file?
- [ ] Rename Container.php to SimpleContainer.php
- [ ] Show stack trace when I have a debug only?
- [ ] PHP 8 named arguments and autowiring.
- [ ] Allow to set definitions via
__constructor
. - [ ] Add supporting Code Driven IoC.
- [ ] Add configurations of Container.
- [ ] Add Performance Benchmarks
License
The MIT License (MIT). Please see the License File for more information.