2017 © Pedro Peláez
 

library fdl-construct-invoker

A ZF2 module that enables argument passing to class constructors with arguments at construction time.

image

franz-deleon/fdl-construct-invoker

A ZF2 module that enables argument passing to class constructors with arguments at construction time.

  • Wednesday, October 23, 2013
  • by franz-deleon
  • Repository
  • 1 Watchers
  • 0 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

FdlConstructInvoker

A ZF2 module that enables argument passing to class constructors (__construct()) that requires or not require arguments at construction time., (*1)

Quick glimpse:, (*2)

// syntax
$this->getServiceLocator()->get('MyInvokableClass')->construct([$firstArg [, $secondArg [, $...]]]);

INSTALLATION:

Register FdlConstructInvoker module in application.config.php. It is important to note that you need to have the module loaded first BEFORE other modules who uses it!, (*3)

    return array(
      'modules' => array(
          'FdlConstructInvoker', // <<---- Needs to be ontop of Application module
          'Application', // I use FdlConstructInvoker so i need to be bellow it!
          ),
      'module_listener_options' => array(
          'module_paths' => array(
              './module',
              './vendor'
          ),
          'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
      )
    );

USAGE:

An example class that you want to register to the service manager.
Notice the class requires a $brick instance in its constructor., (*4)

    namespace class\namespace;
    class Brick
    {
        protected $brick;

        public function __construct($brick)
        {
            $this->brick = $brick;
        }

        public function getBrick()
        {
            return $this->brick;
        }

        public function setBrick($briok)
        {
            $this->brick = $brick;
        }
    }

Register your invokable classes in your modules using the provided getConstructInvokerConfig() method., (*5)

    // Module.php of Application module
    public function getConstructInvokerConfig()
    {
        return array(
            'invokables' => array(
                'brick' => 'class\namespace\Brick',
            ),
        );
    }

Now you can access the class anywhere you have the main ServiceManager. For example, inside a controller.
Take note of the get('brick')->construct() method whick accepts arguments for the constructor., (*6)

    // from an action controller
    public function SomeAction()
    {
        // access it directly from the ServiceManager
        $brick = $this->getServiceLocator()->get('brick')->construct('Concrete Brick');
        echo $brick->getBrick();
        // returns "Concrete Brick"

        // you can also access through the Construct Invoker plugin manager
        $brick2 = $this->getServiceLocator()
                       ->get('constructInvokerPlugin')
                       ->get('brick')
                       ->construct('Hollow Blocks');
        echo $brick2->getBrick();
        // creates new instance of brick object and returns "Hollow Blocks"

        // same instance of brick
        echo $brick->getBrick(); // returns "Concrete Brick"
        $brick->setBrick('Marbles');
        echo $brick->getBrick() // returns "Marbles"
    }

It will also work for regular invokable classes that are not using constructors:, (*7)

    // pretend SomeClass is registered using getConstructInvokerConfig()
    echo $this->getServiceConfig->get('SomeClass')->someMethod(); // whatever somemethod does...

UNDER THE HOOD

FdlConstructInvoker module uses the Peering Service Manager functionality of the Service Manager. Modules who disable this may not be able to do directy below:, (*8)

$this->getServiceLocator()->get('someclass')->construct()

If this is the case. Use the Construct Invoker Plugin Manager:, (*9)

$this->getServiceLocator()->get('constructInvokerPlugin')->get('someclass')->construct();

The Versions

23/10 2013

dev-master

9999999-dev https://github.com/franz-deleon/FdlConstructInvoker

A ZF2 module that enables argument passing to class constructors with arguments at construction time.

  Sources   Download

MIT

The Requires

 

by Franz de Leon

zf2 zend framework construct invoker

29/09 2013

1.0

1.0.0.0 https://github.com/franz-deleon/ConstructInvoker

A ZF2 module that enables argument passing to class constructors (__construct()) that requires or not require arguments at construction time.

  Sources   Download

MIT

The Requires

 

by Franz de Leon

zf2 zend framework zf2 constructor constructor initialization adding constructor arguments