Download the PHP package clancats/container without Composer
On this page you can find all versions of the php package clancats/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package container
ClanCats Container
A PHP Service Container featuring a simple meta-language with fast and compilable dependency injection.
Requires PHP >= 7.4
Pros:
- Minimal overhead and therefore very fast.
- Has no additional dependencies.
- In production serving millions of requests every day.
- Singleton and Factory service resolvers.
- Metadata system allowing very intuitive service lookups.
- A container builder allowing to compile / serialize your service definitions.
- Container files a simple language to define your services and manage your application config.
- Composer integration, allowing you to import service definitions from different packages.
- Lazy service providers for big and dynamic class graphs.
Things you might not like:
- Container allows only named services.
- Currently no auto wiring support.
- Currently no IDE Support for container files.
- Having a meta-language might not meet everyone's taste.
Table of Contents
- Performance
- Installation
- Documentation 💡
- Quick Start ⚡️
- Setup
- Services
- Container file
- Container factory
- Usage Examples
- App Config with Environment
- Aliases / Services Definitions / Parameters Example
- HTTP Routing using Metadata
- Eventlisteners using Metadata
- Logging handler discovery
- Container File Syntax
- Types
- Numbers
- Strings
- Booleans and Null
- Arrays
- Parameters
- Service Definition
- Constructor
- Method calls
- Service metadata
- Imports
- Overriding
- Types
- Example App
- Bootstrap (Container Builder)
- App Container Files
- ToDo / feature whishlist
- Credits
- License
Performance
This package might seem very heavy for a service container, but after a short warmup, the compiled container is blazing fast and has almost no overhead (3 classes/files). Binding and resolving services dynamically is slower but still won't impact performance in a real-world application.
Installation
The container follows PSR-4
autoloading and can be installed using composer:
Syntax Highlighting
I've created a basic tmLanguage definition here: https://github.com/ClanCats/container-tmLanguage
Documentation 💡
The full documentation can be found on clancats.io
Quick Start ⚡️
Following is just a rough example, a much more detailed and explained guide can be found here: Getting Started
Setup
Our target directory structure will look like this:
Services
To demonstrate how to use this service container we need to create two classes a SpaceShip
and a Human
.
Create a new php file src/Human.php
:
Create another php file src/SpaceShip.php
:
Container file
A container file allows you to bind your services & parameters using a simple meta-language.
Note: This feature is entirely optional if you prefer binding your services in PHP itself read: Service Binding
Create a new file called app.ctn
in your applications root folder.
Container factory
Now we need to parse the container file and compile it as a new class. For this task, we create the app.php
file. There you need to require the composer autoloader and require your source files or configure composer to autoload the classes from the src/
directory.
Note: Make sure the
../cache
directory is writable.
The variable $container
contains now a class instance named AppContainer
.
Usage Examples
App Config with Environment
Container parameters are nothing more than values that are globally available in your container. We use them to store most static config values and also to handle different environments.
For this, we usually create two files. In this example:
config.ctn
The main configuration file.config.ctn.env
Environment specific overrides.
config.ctn
:
config.ctn.env
:
In PHP these values are then accessible as parameters. For this, to work you need to configure the correct import paths in your container namespace. You find an example of that in the Example App.
Aliases / Services Definitions / Parameters Example
HTTP Routing using Metadata
Your can use the container metadata to define routes directly with your service definitions:
Now obviously this is depending on your routing implementation. You are able to fetch all services with a routing definition like so:
Example using FastRoute:
Eventlisteners using Metadata
Just like with the routing you can use the meta data system to define eventlisteners:
And then in your event dispatcher register all services that have the matching metadata.
The following example shows how the implementation could look like. Copy pasting this will not just work.
Logging handler discovery
Or maybe you have a custom framework that comes with a monolog logger and you want to make it easy to add custom log handlers per integration:
And your framework can simply look for services exposing a log_handler
meta key:
Container File Syntax
Container files are written in a very simple meta language.
Types
The language supports the following scalar types:
- Strings Single and double quoted.
'hello'
&"world"
- Numbers float / double, int.
3.14
,42
- Booleans
true
andfalse
. - Null
null
- Arrays list and associative.
{'A', 'B', 'C'}
,{'A': 10, 'B': 20}
Numbers
Container files do not differentiate between different number types because it would be an unnecessary overhead, we forward that job directly to PHP.
That means that also the floating point precision is handled by PHP. All values are interpreted means large doubles might be stored rounded.
Strings
Strings must always be encapsulated with a single '
or double "
quote. This serves mainly a comfort purpose when having many quotes inside your string not having to escape them all.
Escaping of special characters works just the usual way.
Beloved or Hated emojis will also work just fine.
Booleans and Null
There is not much to say about them:
Arrays
It's important to notice that all arrays are internally associative. When defining a simple list the associative key is automatically generated and represents the index of the item.
This means that the array {'A', 'B'}
equals {0: 'A', 1: 'B'}
.
Arrays can be defined multidimensional:
Parameters
Parameters or configuration values can also be defined inside the container files.
A parameter is always prefixed with a :
character.
Service Definition
A service definition is always named and must be prefixed with a @
character.
The class name can contain the full namespace.
Constructor
Constructor arguments can be passed after the class name.
Referenced arguments
Arguments can reference a parameter or service.
Method calls
Method calls can be assigned to a service definition.
Service metadata
Metadata can be assigned to every service definition.
Its then possible to fetch the services matching a metadata key.
The metadata key is always a vector / array so you can add multiple of the same type:
The elements inside the metadata definition can have named keys:
Service Updates
It is possible to update already defined services with more construction calls and metadata. This is quite handy to organize large amount of dependencies with a dynamic lookups.
You could for example define your logger in one file.
And add observers using a construction call where you need them.
The same is also true for metadata.
Imports
Other container files can be imported from the container namespace.
Overriding
Services and Parameters have been explicit overwritten if they have already been defined.
Example App
This should showcase a possible structure of an application build using the CCContiner. This is a simplified version of what we use in our private service framework.
Folder structure:
Bootstrap (Container Builder)
This container builder does a few things:
- Imports container namespaces from packages installed using composer.
- Scans the
./app
directory for ctn files. - Adds the env container file to the namespace.
App Container Files
The first file app.ctn
has mainly one job. That is simply to include other files and therefore define the order they are being read.
app.ctn
:
ToDo / feature whishlist
- Container Files
- [x] Metadata support
- [x] Array Support
- [x] Alias Support
- [ ] Container file Namespace support
- [ ] Autowiring by "using trait"
- [ ] Autowiring by "instance of"
- [ ] Autowiring by "has method"
- [ ] Property injection
- [ ] Parameter concatination
- [ ] Input Parameters (used for env detection)
- [x] Late service override (allow for adding meta or calls)
- [ ] macros
- Container
- [x] Metadata support
- [ ] Property injection
Credits
License
The MIT License (MIT). Please see License File for more information.