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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package container

ClanCats Container

A PHP Service Container featuring a simple meta-language with fast and compilable dependency injection.

PHPUnit PHPStan Packagist Packagist GitHub release

Requires PHP >= 7.4

Pros:

Things you might not like:

Table of Contents

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:

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:

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:

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

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of container with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package clancats/container contains the following files

Loading the files please wait ....