calderawp / caldera-containers
Installs: 7 067
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 2
Open Issues: 1
Type:libary
Requires
- php: ^5.6|^7.0
- pimple/pimple: ^3.2
- psr/container: ^1.0@dev
Requires (Dev)
- jakub-onderka/php-parallel-lint: ^1.0
- phpunit/phpunit: 5.0.*
- squizlabs/php_codesniffer: ^3.2
This package is auto-updated.
Last update: 2024-11-09 13:59:03 UTC
README
A collection of useful containers for Caldera (or your) development.
Install
composer require calderawp/caldera-containers
Requires PHP 5.6+
Containers
-
calderawp\CalderaContainers\Container
Basic PSR-11 compatible container decorating Pimple.- Is abstract.
- Converts to arrays.
calderawp\CalderaContainers\Interfaces\Arrayable
- Converts to JSON.
JsonSerializable
-
calderawp\CalderaContainers\ControlledContainer
Extends the base container but only allows in specified attributes.- Is abstract
-
calderawp\CalderaContainers\Service\Coantainer
A basic service container, with provider bindings, lazy-loaded objects, and singletons.
Usage
calderawp\CalderaContainers\Container
@todo
calderawp\CalderaContainers\ControlledContainer
@todo
calderawp\CalderaContainers\Service\Container
Binding As Factory
Add a binding that returns a new object of the same class using the alias std
$container = new \calderawp\CalderaContainers\Service\Container(); $container->bind( 'std', function (){ $obj = new \stdClass(); $obj->foo = rand(); return $obj; }); //$obj1->foo !== $obj2->foo $obj1 = $container->make('std'); $obj2 = $container->make('std');
Binding A Singleton
Add a binding that returns a the same object of the same class using the alias std
. You MUST instantiate class before binding.
$container = new \calderawp\CalderaContainers\Service\Container(); $obj = new \stdClass(); $obj->foo = rand(); $container->singleton( 'std', $obj ); //$obj1->foo === $obj2->foo $obj1 = $container->make('std'); $obj2 = $container->make('std');
Binding A Lazy-Loaded Singleton
Add a binding that returns a the same object of the same class using the alias std
. Class is instantiated 1 times, but is not instantiated until used, if ever.
$container = new \calderawp\CalderaContainers\Service\Container(); $container->singleton( 'std', function (){ $obj = new \stdClass(); $obj->foo = rand(); return $obj; }); //$obj1->foo === $obj2->foo $obj1 = $container->make('std'); $obj2 = $container->make('std'); ## Stuff. Copyright 2018 CalderaWP LLC. License: GPL v2 or later.