PHP code example of molajo / ioc

1. Go to this page and download the library: Download molajo/ioc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

molajo / ioc example snippets


use Molajo\IoC\Container;


    /**
     * Inversion of Control Container
     *
     * @var    object
     * @since  1.0.0
     */
    protected $iocc;



    /**
     * Get a service instance
     *
     * @param    string $service
     * @param    array  $options
     *
     * @results  null|object
     * @since    1.0
     * @throws   FrontcontrollerException
     */
    public function getService($service, $options = array())
    {
        return $this->ioc->getService($service, $options);
    }

    /**
     * Replace the existing service instance
     *
     * @param    string $service
     * @param    object $instance
     *
     * @results  $this
     * @since    1.0
     * @throws   FrontcontrollerException
     */
    public function setService($service, $instance = null)
    {
        $this->ioc->getService($service, $instance);

        return $this;
    }

    /**
     * Clone the existing service instance
     *
     * @param    string $service
     *
     * @results  null|object
     * @since    1.0
     * @throws   FrontcontrollerException
     */
    public function cloneService($service)
    {
        return $this->ioc->cloneService($service);
    }

    /**
     * Remove the existing service instance
     *
     * @param    string $service
     *
     * @results  $this
     * @since    1.0
     */
    public function removeContainerEntries($service)
    {
        $this->ioc->removeContainerEntries($service);

        return $this;
    }


    /**
     * Initialise Application, including invoking Inversion of Control Container and
     *  Services defined in Services.xml
     *
     * @return  $this
     * @since  1.0.0
     * @throws  FrontcontrollerException
     */
    public function initialise()
    {
        $this->checkPHPMinimum();

        set_exception_handler(array($this, 'handleException'));
        set_error_handler(array($this, 'handlePHPErrors'), 0);

        $connect       = $this;
        $getService    = function ($service, $options = array()) use ($connect) {
            return $connect->getService($service, $options);
        };
        $setService    = function ($service, $instance) use ($connect) {
            return $connect->setService($service, $instance);
        };
        $cloneService  = function ($service) use ($connect) {
            return $connect->cloneService($service);
        };
        $removeService = function ($service) use ($connect) {
            return $connect->removeContainerEntries($service);
        };

        $services_folder = 'Molajo\\Services';

        $this->ioc = new Container($getService, $setService, $cloneService, $removeService, $services_folder);

        // Automatically Load These Services
        $xml_string = $this->readXMLFile(__DIR__ . '/' . 'Services.xml');

        $services = simplexml_load_string($xml_string);

        foreach ($services->service as $service) {
            $this->getService((string)$service->attributes()->name, array());
        }

        return;
    }



    $getService = $this->getService;
    $application = $getService('Application');

    /** Has cache been activated? */
    $cache_service = $application->get('cache_service');
    if ((int)$cache_service === 0) {
        return $this;
    }


$getService = $this->getService;

$options = array;
$options['model_type'] = 'Application';
$options['model_name'] = 'Includers';
$database = $getService('LocateFile', $options);


$getService = $this->getService;

$options = array;
$options['if_exists'] = true;
$database = $getService('Molajo\\User', $options);


$getService = $this->getService;

$options = array;
$options['constructor_parameter1'] = true;
$options['constructor_parameter2'] = true;
$database = $getService('Molajo\\Utilities\\Classname', $options);


$setService = $this->setService;
$database = $setService('Application', $instance);


$cloneService = $this->cloneService;
$database = $cloneService('Database');


$removeService = $this->removeService;
$database = $removeContainerEntries('Database');



/**
 * Example Custom Dependency Injection Handler
 *
 * @package    Molajo
 * @license    http://www.opensource.org/licenses/mit-license.html MIT License
 * @copyright  2014-2015 Amy Stephen. All rights reserved.
 */
namespace Molajo\Services\Example;

use Molajo\IoC\AbstractFactoryMethod;
use CommonApi\IoC\FactoryMethodInterface;
use CommonApi\Exception\RuntimeException;

/**
 * Example Custom Dependency Injection Handler
 *
 * @author    Amy Stephen
 * @license    http://www.opensource.org/licenses/mit-license.html MIT License
 * @copyright  2014-2015 Amy Stephen. All rights reserved.
 * @since     1.0
 */
class ExampleFactoryMethod extends AbstractFactoryMethod implements FactoryMethodInterface
{
    /**
     * Constructor
     *
     * @param   $options
     *
     * @since  1.0.0
     */
    public function __construct(array $options = array())
    {

        $this->service_namespace            = 'Molajo\\Example\\Classname';

        // These default to false - set to true here if needed
        $this->store_instance_indicator     = false;
        $this->static_instance_indicator    = false;
        $this->store_properties_indicator   = false;

        parent::__construct($options);
    }

    /**
     * Follows instantiation of the service class and before the method identified as the "start" method
     *
     * @return  object
     * @since  1.0.0
     */
    public funonBeforeInstantiationtances()
    {
        retonBeforeInstantiationncyInstances();
    }

    /**
     * Instantiate Class
     *
     * @param   bool $create_static
     *
     * @return  $this
     * @since  1.0.0
     * @throws  \CommonApi\Exception\RuntimeException
     */
    public function instantiateClass()
    {
        return parent::instantiate($create_static);
    }

    /**
     * Follows the completion of the instantiate service method
     *
     * @return  object
     * @since  1.0.0
     * @throws  \CommonApi\Exception\RuntimeException
     */
    public function onAfterInstantiation()
    {
        return parent::onAfterInstantiation();
    }

    /**
     * Initialise Service Class, if the method exists
     *
     * @return  object
     * @since  1.0.0
     * @throws  \CommonApi\Exception\RuntimeException
     */
    public function initialise()
    {
        return parent::initialise();
    }

    /**
     * Follows the completion of Initialise
     *
     * @return  object
     * @since  1.0.0
     * @throws  \CommonApi\Exception\RuntimeException
     */
    public function onAfterServiceInitialise()
    {
        return parent::onAfterServiceInitialise();
    }

    /**
     * Get Product Result
     *
     * @return  object
     * @since  1.0.0
     * @throws  \CommonApi\Exception\RuntimeException
     */
    public function getProductValue()
    {
        return parent::getProductValue();
    }
}



    curl -s https://getcomposer.org/installer | php

{
    "  "Molajo/Ioc": "1.*"
    }
}

    php composer.phar install

Molajo
.. Services
.. .. Database
.. .. .. DatabaseFactoryMethod.php
.. .. Configuration
.. .. .. ConfigurationFactoryMethod.php
.. .. Modelread
.. .. .. ModelreadFactoryMethod.php
.. .. etc ..


Molajo
.. Services
.. .. ServiceName
.. .. .. ServiceNameFactoryMethod.php