ZF2 Dependency Injection of Module Service without

2019-08-24 03:33发布

问题:

I am trying to create an instance of a service which I have implemented in ZF2.

Module.php

namespace ProductImage;

use Zend\Db\ResultSet\ResultSet; use ProductImage\Model\ProductImageStorage; use ProductImage\Service\ProductImageService; use Zend\ModuleManager\Feature\ServiceProviderInterface; use Zend\Db\TableGateway\TableGateway; use ProductImage\Model\ProductImageTable; use ProductImage\Model\ProductImage;

class Module implements ServiceProviderInterface {
    /* Invoked by Module Manager */
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__  => __DIR__ . '/src/' . __NAMESPACE__,
                    'ImageResizer' => __DIR__ . '/src/ImageResizer'
                ),
            ),
        );
    }

    /* Invoked by Module Manager */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    /* Configure DB Service Managers */
    public function getServiceConfig()
    {
        return array(
            'factories'  => array(
                'ProductImage\Model\ProductImageTable'     => function($sm)
                {
                    $tableGateway = $sm->get('ProductImageTableGateway');
                    $table = new ProductImageTable($tableGateway);
                    return $table;
                },
                'ProductImageTableGateway'                 => function ($sm)
                {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new ProductImage());
                    return new TableGateway('F3G_IMAGES', $dbAdapter, null, $resultSetPrototype);
                }
            ),
            'invokables' => array(
                'productImageService'=> function ($sm)
                {
                    $productImageTable = $sm->get('ProductImageTable\Model\ProductImageTable');
                    return new ProductImageService($productImageTable);
                }
            ),
        );
    } }

ProductImage\Service\ProductImageService

class ProductImageService implements ServiceLocatorAwareInterface
{
    protected $productImageTable;
    protected $services;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->services = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->services;
    }

    function __construct(ProductImageTable $productImageTable)
    {
        $this->productImageTable = $productImageTable;
    }
    // ...
}

Instantiation of service using DI in stand-alone php file (functions.inc.php)

//initialise a standard loader
set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/lib/zendframework/library/');
require_once 'Zend/Loader/AutoloaderFactory.php';
use Zend\Loader\AutoloaderFactory;
Zend\Loader\AutoloaderFactory::factory(array(
        'Zend\Loader\StandardAutoloader' => array(
            'autoregister_zf' => TRUE,
        )
    )
);
$di = new Zend\Di\Di();
$piService = 
$di->get('ProductImage\Service\ProductImageService'); // (< throws Zend\\Di\\Exception\\ClassNotFoundException)
var_export($piService);
die();

Error thrown by $di->get:

[Wed Oct 02 10:37:41 2013] [error] [client 192.168.6.52] PHP Fatal error:  Uncaught exception 'Zend\\Di\\Exception\\ClassNotFoundException' with message 'Class ProductImage\\Service\\ProductImageService could not be located in provided definitions.' in /usr/lib/zendframework/library/Zend/Di/Di.php:264\nStack trace:\n#0 /usr/lib/zendframework/library/Zend/Di/Di.php(227): Zend\\Di\\Di->newInstance('ProductImage\\Se...', Array, true)\n#1 /home/app/public_html/wp-content/themes/manage-product-images/functions.inc.php(34): Zend\\Di\\Di->get('ProductImage\\Se...')\n#2 /home/app/public_html/wp-content/themes/functions.inc.php(14): require_once('/home...')\n#3 /home/app/public_html/wp-content/themes/functions.php(14): require_once('/home...')\n#4 /home/app/public_html/wp-settings.php(293): include('/home...')\n#5 /home/app/public_html/wp-config.php(90): require_once('/home...')\n#6 /home/app/public_html/w in /usr/lib/zendframework/library/Zend/Di/Di.php on line 264

Looking through the docs (http://framework.zend.com/manual/2.1/en/tutorials/quickstart.di.html), it seems like I need a Bootstrap.php in my Module to configure the DI. Is there a different way of doing this as I do not have this file and no idea what it should look like?