Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2.  
  3. // ARQUIVO ContainerConfig.php
  4. namespace Igor\Config;
  5.  
  6. use Pimple\Container;
  7.  
  8. /**
  9.  * Configuração do container de dependências.
  10.  */
  11. class ContainerConfig
  12. {
  13.  
  14.     /**
  15.      *
  16.      * @return \Pimple\Container
  17.      */
  18.     public static function getConfig()
  19.     {
  20.         $container = new Container();
  21.  
  22.         /**
  23.          * Twig
  24.          */
  25.         // Template loader
  26.         $container['twig_loader'] = function($c) {
  27.             $cfg = TwigConfig::getConfig();
  28.  
  29.             return new \Twig_Loader_Filesystem($cfg['templates_path']);
  30.         };
  31.         // Instância compartilhada do Twig
  32.         $container['twig'] = function($c) {
  33.             $cfg = TwigConfig::getConfig();
  34.  
  35.             return new \Twig_Environment(
  36.                 $c['twig_loader'],
  37.                 $cfg['enviroment']
  38.             );
  39.         };
  40.         // Factory do Twig
  41.         $container['twig_factory'] = $container->factory(
  42.             function ($c) {
  43.                 $cfg = TwigConfig::getConfig();
  44.  
  45.                 return new \Twig_Environment(
  46.                     $c['twig_loader'],
  47.                     $cfg['enviroment']
  48.                 );
  49.             }
  50.         );
  51.         /**
  52.          * Request
  53.          */
  54.         $container['request'] = function ($c) {
  55.             $cfg = MainConfig::getConfig();
  56.  
  57.             return new \Igor\Library\Http\Request(
  58.                 $cfg['request']['baseurl'],
  59.                 $cfg['request']['uri'],
  60.                 $cfg['request']['params']
  61.             );
  62.         };
  63.         /**
  64.          * Dispatcher
  65.          */
  66.         $container['dispatcher'] = function ($c) {
  67.             return new \Igor\Library\Dispatcher();
  68.         };
  69.         /**
  70.          * Response
  71.          */
  72.         $container['response'] = function ($c) {
  73.             return new \Igor\Library\Http\Response();
  74.         };
  75.         /**
  76.          * Router
  77.          */
  78.         $container['router'] = function ($c) {
  79.             $cfg = RouterConfig::getConfig();
  80.  
  81.             return new \Igor\Library\Route\Router($cfg);
  82.         };
  83.         /**
  84.          * Front controller
  85.          */
  86.         $container['front_controller'] = function ($c) {
  87.             return new \Igor\Library\Controller\FrontController(
  88.                 $c['router'], $c['dispatcher']
  89.             );
  90.         };
  91.        
  92.         return $container;
  93.     }
  94.    
  95. }
  96.  
  97. // ARQUIVO index.php
  98. require 'vendor/autoload.php';
  99.  
  100. use Igor\Config\ContainerConfig;
  101.  
  102. // O controller envia saída direto para o browser, então o Output Buffer deve
  103. // ser ativado para que seja possível enviar enviar os cabeçalhos de resposta
  104. // no final da execução da aplicação.
  105. ob_start();
  106.  
  107. $container = ContainerConfig::getConfig();
  108.  
  109. $request = $container['request'];
  110. $response = $container['response'];
  111.  
  112. $frontController = $container['front_controller'];
  113. $frontController->run($request, $response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement