Using the PHP Toolkit in ZF2 without using a closure

Discussion in 'Zend Framework 2 (ZF2) & Apigility' started by kingston, Apr 24, 2015.

  1. kingston

    kingston Administrator Staff Member

    I just realized I am using a closure to setup my service for the PHP Toolkit... in general a no no!

    Need help creating a factory to do this. How would I set this up?

    PHP:
        'service_manager' => array(
            'invokables' => array(
                'AuthorizationIbmi' => 'authorization.php',
            ),
            'factories' => array(
                // Toolkit object to use later
                'tkconn' => function ($sm) {
                    // get adapter as defined above
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    // get real database connection from the adapter
                    $dbAdapter->getDriver()
                        ->getConnection()
                        ->connect();
                    $dbConn = $dbAdapter->getDriver()
                        ->getConnection()
                        ->getResource();
                    require_once("ToolkitService.php");
                    // pass database connection into toolkit instantiation
                    $tkConn = \ToolkitService::getInstance($dbConn, DB2_I5_NAMING_ON); // cheating here by hard-coding naming mode
                    $tkConn->setOptions(array(
                        'stateless' => true
                    ));
                    return $tkConn;
                }
            )
        ),
     
  2. kingston

    kingston Administrator Staff Member

    I was able to do this! Basically you want to take everything within the closure (the part that is function ($sm) {...}) and put it in its own class.

    I created a file called RPG\FactoryService\ToolkitFactory.php and here is the code:
    PHP:
    <?php

    namespace RPG\ServiceFactory;

    use Zend\ServiceManager\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;

    class ToolkitFactory implements FactoryInterface
    {
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            $dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
            $dbAdapter->getDriver()
                ->getConnection()
                ->connect();
            $dbConn = $dbAdapter->getDriver()
                ->getConnection()
                ->getResource();
            require_once("ToolkitService.php");
            // pass database connection into toolkit instantiation
            $tkConn = \ToolkitService::getInstance($dbConn, DB2_I5_NAMING_ON); // cheating here by hard-coding naming mode
            $tkConn->setOptions(array(
                'stateless' => true
            ));
            return $tkConn;
        }
    }
    And then in my module.config.php

    PHP:
        'service_manager' => array(
            'factories' => array(
                // Toolkit object to use later
                'tkconn' => 'RPG\ServiceFactory\ToolkitFactory'
            )
        ),
     
  3. John Taylor

    John Taylor New Member

    Kingston,

    What directory would you put that RPG\FactoryService\ToolkitFactory.php file in?
     

Share This Page