PHP Classes

How to Implement PHP Dependency Injection with Parameters Loaded from Separate Files Using the Package PHP Dependency Injection: Access services using JSON configuration files

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-09-14 (24 days ago) RSS 2.0 feedNot enough user ratingsTotal: 20 This week: 2All time: 11,289 This week: 16Up
Version License PHP version Categories
php-dependency-injec 1.0MIT/X Consortium ...8Language, Design Patterns, PHP 8
Description 

Author

This package can access services using JSON configuration files.

It allows to define services that are implemented using service classes.

The services can be configured using JSON files that define the names of the services and other configuration aspects.

Applications can access the configured services using the container class to load and initialize the service object.

Picture of Cristian Radu
Name: Cristian Radu <contact>
Classes: 4 packages by
Country: Romania Romania
Age: ???
All time rank: 438580 in Romania Romania
Week rank: 7 Up1 in Romania Romania Up

Documentation

PHP Dependency Injection Container

Dependency injection container class for php. Extends kris-ro/php-config functionality and relies on it for configuration.

Installation

Use composer to install PHP Dependency Injection Container.

composer require kris-ro/php-dependency-injection

Configuration

The Container reads the definitoin for the requested service from the config array built by Container::buildConfig().\ All services must be defined in the services branch of the configuration array. Each service is identified by its service idendifier (service_identifier in the example below).\ First key in the definition array must be the class containing the class name prefixed by the namespace and optionally sufixed with the method name. Valid examples:

{
  "services" : {
    "service_identifier" : {
      "class" : "\\Class\\With\\Namespace"
    },
    "another_service_identifier" : {
      "class" : "\\Class\\With\\Namespace::MethodName"
    }
  }
}

The class can be followed by other entries in the definition array representing the methods of the created service that will be executed (in the same order as they are listed in the definition) by the container before the service is delivered. Valid examples:

{
  "services" : {
    "myPDO" : {
      "class": "\\PDO",
      "_construct": {
        "dsn": "mysql:host=localhost;dbname=test",
        "username": "k",
        "password": "123456"
      }
    },
    "validator" : {
      "class": "\\KrisRo\\Validator\\Validator",
      "createRegexRules" : {
        "rules": {
          "alphanumeric": "/^[a-z0-9\\-_]+$/i"
        }
      }
    }
  }
}

As you can see above, for myPDO the _construct is specified because it needs those three arguments.\ The second definition validator also specifies a method to be executed with one argument rules.

The names of the arguments in the definition are taken from the method.\ If you look at the PDO definition above you'll see that _constructor has three arguments dsn, username and password that are maped to PDO's constructor arguments $pdo, $username and $password respectively.

There are three types of argument values: - a service identifier prefixed with the character @ - an entry path from the config array prefixed with the character # - anything else is passed as is

Service as argument\ Value of credentialsOrPDO references myPDO service.

{
  "services" : {
    "myPDO" : {
      "class": "\\PDO",
      "_construct": {
        "dsn": "mysql:host=localhost;dbname=test",
        "username": "k",
        "password": "123456"
      }
    },
    "model" : {
      "class": "\\KrisRo\\PhpDatabaseModel\\Model",
      "_construct": {
        "credentialsOrPDO": "@myPDO"
      }
    }
  }
}

Entry path from config array\ Value of rules references validator > rules entry in configuration array.

{
  "validator" : {
    "rules": {
      "alphanumeric": "/^[a-z0-9\\-_]+$/i"
    }
  },
  "services" : {
    "validator": {
      "class": "\\KrisRo\\Validator\\Validator",
      "_construct": {
        "rules": "#validator/rules"
      }
    }
  }
}

Usage

Once the service is defined it is as simple as:

use KrisRo\PhpDependencyInjection\Container;

# first build the configuration array. See more at https://github.com/kris-ro/php-config
Container::buildConfig('/absolute/path/to/your/folder/with/json/files');

# load the service you need
$pdo = Container::service('myPDO');
$model = Container::service('model');
$validator = Container::service('validator');

  Files folder image Files (6)  
File Role Description
Files folder imageconfigs (2 files)
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (6)  /  configs  
File Role Description
  Accessible without login Plain text file local.json Data Auxiliary data
  Accessible without login Plain text file services.json Data Auxiliary data

  Files folder image Files (6)  /  src  
File Role Description
  Plain text file Container.php Class Class source

  Files folder image Files (6)  /  tests  
File Role Description
  Plain text file ContainerTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:20
This week:2
All time:11,289
This week:16Up