{% extends '../../base.html.twig' %}

{% block body %}
  <div class="container-fluid">
    <div class="row flex-xl-nowrap">
      {% include '../../../documentation/navbar.html.twig' with {
        'route' : nodefony.route
      } %}
      <main class="col-12 col-md-9 col-xl-8 py-md-3 pl-md-5" style="top: 5rem;">

        <div class="page-header">
          <h1><strong>{{ "Dependency Injections" | trans() }} </strong> </h1>
        	<p>
        		A Dependency Injections service  is an entity that can manage instance of services in application .<br>
        		 .<br>
        	</p>


        	<p>
        		<center>
        			<img src="/documentation-bundle/images/injector.png" />
        		</center>
        	</p>
        	<p>
        	</p>

        	<div class="alert alert-warning">
        		Nodefony don't manage Dependency Injections like symfony2 framework but is inspired  <br>
        	</div>

          <h2 > You can see here :</h2>

          <p>
          	<ul>
          		<li> <a href="#location">Location Services</a></li>
          		<li> <a href="#file">File Services</a></li>
          		<li> <a href="#service"> Services Usage</a></li>
          	</ul>
          <p>

          <h2  id="location">Location  Services:</h2>
          <div class="doc2">
          	<p>
          		You can find or add service in bundle repository under <code>services</code> directory .

          	</p>

          	<pre><code class="hljs console">$ ls -l src/nodefony/bundles/hello-bundle

          drwxr-xr-x    4 cci  staff     128 27 mar 15:13 Command
          drwxr-xr-x    3 cci  staff      96 27 mar 15:13 Entity
          drwxr-xr-x    6 cci  staff     192 27 mar 15:13 Resources
          drwxr-xr-x    3 cci  staff      96 27 mar 15:13 build
          drwxr-xr-x    3 cci  staff      96 27 mar 15:13 controller
          drwxr-xr-x    3 cci  staff      96 27 mar 15:13 doc
          -rw-r--r--    1 cci  staff     642 27 mar 15:13 helloBundle.js
          drwxr-xr-x  646 cci  staff   20672 27 mar 15:14 node_modules
          -rw-r--r--    1 cci  staff  297699 27 mar 15:14 package-lock.json
          -rw-r--r--    1 cci  staff    1274 27 mar 15:14 package.json
          -rw-r--r--    1 cci  staff       1 27 mar 15:13 readme.md
          drwxr-xr-x    3 cci  staff      96 27 mar 15:13 <h3 >services</h3>
          drwxr-xr-x    2 cci  staff      64 27 mar 15:13 src
          drwxr-xr-x    3 cci  staff      96 27 mar 15:13 tests

          	</code></pre>
          </div>


          <h3>Autoloader  :</h3>
          <div class="doc3 ">

          <p>
          	The core autoloader nodefony load all files in this directory which match with <strong>'...Service.js'</strong> <br>
          </p>


          </div>

          <h3>Interpreter  :</h3>
          <div class="doc3 ">

          <p>
          	The core autoloader nodefony interpret the javascript code in an extended global context.<br>
          	You can find namespace nodefony in the global context .
          </p>


          </div>



          <h3>Class Service  :</h3>
          <div class="doc3 ">
          <p>
              The name of file must finish by <strong>'...Service.js'</strong>  example : <code> name<strong>Service.js</strong></code><br>
          </p>
          	<pre><code class="hljs javascript">
          const tab = [1,2,3,4];
          const myFuntion = function(){
          	return tab ;
          }

          /*
           *	Class service name
           *	constructor
           */
          module.exports = class <strong>name</strong> extends nodefony.Service {

          	/*
           	*	constructor  service
           	*/
          	constructor (kernel, injection2){
                  super("nameService", kernel.container, kernel.notificationsCenter);
                  this.injection2 = injection2 ;
          	};

          	/*
           	*	Method  service
           	*/
          	runTask (domain){
              console.log(domain);
          	};
          };

          	</code></pre>
          </div>

          <h3>Configuration Injection Service  :</h3>
          <div class="doc3 ">
          <p>
              Now the injection service can manage this service to instanciate this,  and inject dependencies ( kernel, injection2 ...).<br>
          </p>
          <code> Ressources/config/<strong>services.js</strong></code><br>
          <pre><code class="hljs javascript">
          module.exports = {
            name: {
              class: nodefony.services.name,
              arguments: ["@kernel", "@injection2"],
              calls: [{
                method: "runTask",
                arguments: [kernel.domain]
              }]
            }
          };
          </code></pre>
          </div>


          <h2 name="file" id="file"> Example Services bundle core  http  :<h2>
          <h3>File Services  :</h3>
          <div class="doc3 ">

          <p>
          	The name of file must finish by <strong>'...Service.js'</strong>  example : <code> name<strong>Service.js</strong></code><br>
          	Exemple http-bundle :
          </p>

          	<pre><code class="hljs console">$ ll http-bundle/services

          ├── kernel
          │   └── httpKernelService.js
          ├── servers
          │   ├── httpService.js
          │   ├── httpsService.js
          │   ├── webSocketSecureService.js
          │   └── websocketService.js
          ├── sessions
          │   └── sessionsService.js
          ├── staticFiles
          │   ├── staticService.js
          │   └── staticsService.js.old
          └── upload
              └── uploadService.js
          	</code></pre>

          </div>


          <h3>Configuration Injection  :</h3>
          <div class="doc3 ">
          	<p>
          		Exemple httpBundle  : <code>services.js</code>
          	</p>
          	<pre><code class="hljs javascript">module.exports = {
            serverStatics: {
              class: nodefony.services.serverStatics,
              arguments: ["@container"]
            },
            httpKernel: {
              class: nodefony.services.httpKernel,
              arguments: ["@container", "@serverStatics"]
            },
            httpServer: {
              class: nodefony.services.httpServer,
              arguments: ["@httpKernel"]
            },
            httpsServer: {
              class: nodefony.services.httpsServer,
              arguments: ["@httpKernel"]
            },
            websocketServer: {
              class: nodefony.services.websocketServer,
              arguments: ["@httpKernel"]
            },
            websocketServerSecure: {
              class: nodefony.services.websocketServerSecure,
              arguments: ["@httpKernel"]
            },
            sessions: {
              class: nodefony.services.sessions,
              arguments: ["@httpKernel"]
            },
            upload: {
              class: nodefony.services.upload,
              arguments: ["@httpKernel"]
            },
            sockjs: {
              class: nodefony.services.sockjs,
              arguments: ["@httpKernel", "@httpServer", "@httpsServer"]
            },
          };
          	</code></pre>

          </div>

          <h3>Starting Log   :</h3>
          <div class="doc3 ">

          <p>
          	When starting all service are registred in Global Container :
          </p>

          	<pre><code class="hljs console">Thu Mar 28 2019 11:45:38 DEBUG KERNEL :  REGISTER BUNDLE : http
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : httpKernel
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : requestClient
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : httpServer
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : httpsServer
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : sockjs
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : serverStatics
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : websocketServerSecure
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : websocketServer
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : sessions
          Thu Mar 28 2019 11:45:38 DEBUG BUNDLE http : Register Service : upload
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE KERNEL READER : HTTP SERVICE LOAD FILE :nodefony-starter/node_modules/@nodefony/http-bundle/Resources/config/services.js
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE SERVER STATICS  : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE HTTP KERNEL  : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE SERVER HTTP  : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE SERVER HTTPS  : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE SERVER WEBSOCKET  : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE SERVER WEBSOCKET SECURE  : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE SESSIONS  : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG HTTP UPLOAD : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE sockjs  : STARTED
          Thu Mar 28 2019 11:45:38 DEBUG SERVICE requestClient  : STARTED
          	</code></pre>
          </div>


          <h2  id="service">Services Usage  :</h2>

          <div class="doc2">
          	<p>
          	</p>

          	<h3 name="" id="">Get service in  controller:</h3>

          	<div class="doc3">
          		<p>
          			To use service you must pass by Services Container  :<br>
          			Example to use service in action controller : <br>
          			The controller core have <strong>get</strong> method to find a service
          		</p>

          		<pre><code class="hljs javascript">footerAction (){
            // get sessions service
            <h3>let sessionService = this.get("sessions");</h3>

            // get translation  service
            let translateService = this.get("translation");
            let langs = translateService.getLangs();
            let locale = translateService.getLocale();

          	...
          }
          		</code></pre>
          	</div>
          </div>


        </div>


      </main>
    </div>
  </div>
{% endblock %}
