Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, this is how I did it:</p> <p>As IoC Framework I used <a href="http://components.symfony-project.org/dependency-injection/" rel="noreferrer">this component of the symfony framework</a> (but I didnt download the latest version, I used an older one I used on projects before... keep that in mind!). I added its classes under <code>/library/ioc/lib/</code>.</p> <p>I added these init function in my <code>Bootstrap.php</code> in order to register the autoloader of the IoC framework:</p> <pre><code>protected function _initIocFrameworkAutoloader() { require_once(APPLICATION_PATH . '/../library/Ioc/lib/sfServiceContainerAutoloader.php'); sfServiceContainerAutoloader::register(); } </code></pre> <p>Next, I made some settings in <code>application.ini</code> which set the path to the wiring xml and allow to disable automatic dependency injection e. g. in unit tests:</p> <pre><code>ioc.controllers.wiringXml = APPLICATION_PATH "/objectconfiguration/controllers.xml" ioc.controllers.enableIoc = 1 </code></pre> <p>Then, I created a custom builder class, which extends <code>sfServiceContainerBuilder</code> and put it under <code>/library/MyStuff/Ioc/Builder.php</code>. In this test project I keep all my classes under <code>/library/MyStuff/</code>.</p> <pre><code>class MyStuff_Ioc_Builder extends sfServiceContainerBuilder { public function initializeServiceInstance($service) { $serviceClass = get_class($service); $definition = $this-&gt;getServiceDefinition($serviceClass); foreach ($definition-&gt;getMethodCalls() as $call) { call_user_func_array(array($service, $call[0]), $this-&gt;resolveServices($this-&gt;resolveValue($call[1]))); } if ($callable = $definition-&gt;getConfigurator()) { if (is_array($callable) &amp;&amp; is_object($callable[0]) &amp;&amp; $callable[0] instanceof sfServiceReference) { $callable[0] = $this-&gt;getService((string) $callable[0]); } elseif (is_array($callable)) { $callable[0] = $this-&gt;resolveValue($callable[0]); } if (!is_callable($callable)) { throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service))); } call_user_func($callable, $service); } } } </code></pre> <p>Last, I created a custom controller class in <code>/library/MyStuff/Controller.php</code> which all my controllers inherit from:</p> <pre><code>class MyStuff_Controller extends Zend_Controller_Action { /** * @override */ public function dispatch($action) { // NOTE: the application settings have to be saved // in the registry with key "config" $config = Zend_Registry::get('config'); if($config['ioc']['controllers']['enableIoc']) { $sc = new MyStuff_Ioc_Builder(); $loader = new sfServiceContainerLoaderFileXml($sc); $loader-&gt;load($config['ioc']['controllers']['wiringXml']); $sc-&gt;initializeServiceInstance($this); } parent::dispatch($action); } } </code></pre> <hr /> <p>What this basically does is using the IoC Framework in order to initialize the already created controller instance (<code>$this</code>). Simple tests I did seemed to do what I want... let´s see how this performs in real life situations. ;)</p> <p>It´s still monkey patching somehow, but the Zend Framework doesn´t seem to provide a hook where I can create the controller instance with a custom controller factory, so this is the best I came up with...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload