Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From my understanding in Silex/Symfony2, is that there is no 'magical way' to do these type of things.</p> <p>For my <strong>first</strong> question: It is allowed to add objects that are created before the container.</p> <p>In Symfony2, in <code>Kernel:initializeContainer</code> function, Kernel adds itself to the container (<code>$this-&gt;container-&gt;set('kernel', $this);</code>) and later on, in the xml files, services are injected with Kernel (<code>&lt;argument id="kernel" type="service" /&gt;</code>).</p> <p>In Silex, <code>Application:__construct</code> function creates and adds objects to the container. <code>Application</code> injects itself to the <code>ServiceProviders</code>, so these providers can inject dependencies to their objects and add them to the container.</p> <p><code>$container-&gt;manualAdd('_logger', $logger);</code> <strong>is correct</strong>.</p> <hr> <p>For my <strong>second</strong> question: depends on how I want to handle it. I came up with 3 options:</p> <p><strong>.1</strong> For C#-event-like, <code>Kernel</code> is added into the container:</p> <p>In ComplexClass: <code>kernel.terminate += kernelTerminate</code></p> <p><strong>.2</strong> EventDispatcher (as long as <code>EventDispatcher</code> class doesn't need dependencies that are created in the xml files)</p> <pre><code>//bootstrap function: $dispatcher = new KernelEventDispatcher(); $kernel = new Kernel($dispatcher); $container-&gt;manualAdd('_kernel.dispatcher'); </code></pre> <p>.</p> <pre><code>&lt;!-- in file.xml:ComplexClass !--&gt; &lt;argument id="_kernel.dispatcher" type="service" /&gt; </code></pre> <p><strong>.3</strong> Create an object that implements an interface: (I couldn't find another name for <code>Runnable</code>)</p> <pre><code>&lt;!-- in file.xml !--&gt; &lt;service id="complex_class_runnable" class="ComplexClassRunnable"&gt; &lt;argument type="service" id="complex_class" /&gt; &lt;argument type="service" id="_kernel" /&gt; &lt;/service&gt; </code></pre> <p>.</p> <pre><code>//in ComplexClassRunnable $kernel-&gt;addRunnable($this); </code></pre> <p>.</p> <pre><code>//in Kernel foreach($this-&gt;runnables as $runnable){ $runnable-&gt;init(); //same for terminate } </code></pre> <p>Bootstrap updated:</p> <pre><code>function bootstrap($file = 'file.xml'){ $logger = new Logger(); $logger-&gt;log('bootstrap: init'); $kernel = new Kernel($logger); $container = new DIContainer(new ConfigReader($file), $logger); $container-&gt;manualAdd('_kernel', $kernel); $container-&gt;manualAdd('_logger', $logger); $container-&gt;build(); $kernel-&gt;boot(); $logger-&gt;log('bootstrap: terminate'); } class DIContainer{ public function build(){ //read xmls and create classes, etc. $this-&gt;logger-&gt;log('DIContainer: creating objects: {objects}'); } } class Kernel{ public function boot(){ //... } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    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