2017 © Pedro Peláez
 

library point-core

PHP IoC/DI Container Framework

image

samejack/point-core

PHP IoC/DI Container Framework

  • Wednesday, July 18, 2018
  • by samejack
  • Repository
  • 1 Watchers
  • 2 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 150 % Grown

The README.md

point-core

Latest Stable Version Build Status Coverage Status, (*1)

Overview

This is a PHP IoC/DI Module Container.It can inject instance of object through the @Annotation on PHPDoc., (*2)

Features

  • Dependency Injection
  • Inversion of Control
  • File/Class Lazy Loading
  • Configuration by PHP Annotation
  • Rich Module Framework

Getting and Start (composer)

Install point-core from composer repository

composer require samejack/point-core

Get first object from context

<?php

include 'vendor/autoload.php';

use point\core\Context;
use point\core\Bean;

class Bar
{
  public function __toString()
  {
    return 'Bar!';
  }
}

$context = new Context();

$context->addConfiguration(array(
  array(
    Bean::CLASS_NAME => 'Bar'
  )
));

$bar = $context->getBeanByClassName('Bar');
echo $bar;  // print Bar!

Annotaion Specification

Annotation Description
@Autowired Auto inject
@Autoload Auto load class file without bean defined
@var Mapping by class or interface name
@Qualifier Inject by identify

Bean Configuration

Configuration Description Optional
Bean::CLASS_NAME Class name of bean
Bean::ID Inject object by ID via @Qualifier V
Bean::INIT-METHOD Initialize invoke function V
Bean::SCOPE Instance mode (prototype or singleton) V
Bean::CONSTRUCTOR_ARG Constructor arguments V
Bean::PROPERTY Set default property V
Bean::AUTO_LOAD Auto load class when be dependent on V
Bean::INCLUDE_PATH Auto include file path (use context autoload before SPL) V

PHP Example (Snippet Code):

General Inject

<?php

use point\core\Context;
use point\core\Bean;

class Foo
{
  /**
   * @Autowired
   * @var Bar
   */
  private $_bar;

  public function getBar()
  {
      return $this->_bar;
  }
}

class Bar
{
}

$context = new Context();

$context->addConfiguration(array(
  array(
    Bean::CLASS_NAME => 'Foo'
  ),
  array(
    Bean::CLASS_NAME => 'Bar'
  )
));

$foo = $context->getBeanByClassName('Foo');
var_dump($foo->getBar());  // print Class Bar

Inject After

<?php

use point\core\Context;
use point\core\Bean;

class Foo
{
  /**
   * @Autowired
   * @var Bar
   */
  private $_bar;

  public function getBar()
  {
      return $this->_bar;
  }
}

$context = new Context();

$context->addConfiguration(array(
  array(
    Bean::CLASS_NAME => 'Foo'
  )
));

$foo = $context->getBeanByClassName('Foo');

var_dump($foo->getBar());  // print NULL on unload Bar Class

// load Bar class
class Bar
{
}

// set configuration
$context->addConfiguration(array(
    array(
        Bean::CLASS_NAME => 'Bar'
    )
));

var_dump($foo->getBar());  // print Class Bar

Inject by ID of Bean


use point\core\Context; use point\core\Bean; class Foo { /** * @Qualifier("bar.2") * @var Bar */ private $_bar; public function getBar() { return $this->_bar; } } class Bar { private $_name; public function __construct($name) { $this->_name = $name; } public function toString() { return $this->_name; } } $context = new Context(); $context->addConfiguration(array( array( Bean::CLASS_NAME => 'Foo' ), array( Bean::CLASS_NAME => 'Bar', Bean::ID => 'bar.1', Bean::CONSTRUCTOR_ARG => ['i am first.'] ), array( Bean::CLASS_NAME => 'Bar', Bean::ID => 'bar.2', Bean::CONSTRUCTOR_ARG => ['i am second.'] ) )); $foo = $context->getBeanByClassName('Foo'); var_dump($foo->getBar()); // print Class Bar

PDODB inject example

class MyControllerA
{
    /**
    * @Autowired
    * @var PDO
    */
    private $_pdo;
    public function getPdo()
    {
        return $this->_pdo;
    }
}

class MyControllerB
{
    /**
    * @Autowired
    * @var PDO
    */
    private $_pdo;
    public function getPdo()
    {
        return $this->_pdo;
    }
}

$context = new Context();
$context->addConfiguration(array(
    array(
        Bean::CLASS_NAME => 'MyControllerA'
    ),
    array(
        Bean::CLASS_NAME => 'MyControllerB'
    ),
    array(
        Bean::CLASS_NAME => 'PDO',
        Bean::CONSTRUCTOR_ARG => ['mysql:host=localhost;dbname=mysql', 'root', 'password!']
    )
));

$ctrlA = $context->getBeanByClassName('MyControllerA');
var_dump($ctrlA->getPdo());  // print: class PDO#11 (0)...
$ctrlB = $context->getBeanByClassName('MyControllerB');
var_dump($ctrlB->getPdo());  // print: class PDO#11 (0)...

License

Apache License 2.0, (*3)

The Versions

18/07 2018

dev-master

9999999-dev

PHP IoC/DI Container Framework

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by SJ

framework php ioc di point-core

29/04 2018

1.0.1

1.0.1.0

PHP IoC/DI Container Framework

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by SJ

framework php ioc di point-core

31/01 2017

1.0.0

1.0.0.0

PHP IoC/DI Container Framework

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by SJ

framework php ioc di point-core

18/12 2016

0.9.1

0.9.1.0

PHP IoC/DI Container Framework

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by SJ

framework php ioc di point-core

18/05 2015

0.9.0

0.9.0.0

PHP IoC/DI Container

  Sources   Download

Apache-2.0

The Development Requires

by SJ

framework point-core