Guest User

Untitled

a guest
Nov 18th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Application ;
  4.  
  5. use Exception ;
  6. use ErrorException ;
  7. use Application\Core\Object ;
  8. use Application\Core\ApplicationConfig ;
  9. use Application\Library\Registry\Registry ;
  10. use Application\Library\RequestParser\RequestParser ;
  11. use Application\Library\Session\SessionHandler ;
  12. use Application\Library\Cookie\CookieHandler ;
  13. use Application\MVC\Models\Database\MySQL\MySQLConnection ;
  14. use Application\MVC\Controllers\ControllerManager ;
  15. use Application\MVC\Views\ErrorView ;
  16. use Application\MVC\Controllers\HomeController ;
  17. use Application\MVC\Controllers\LoginController ;
  18. use Application\MVC\Controllers\CategoryController ;
  19. use Application\MVC\Controllers\ProductController ;
  20. use Application\MVC\Controllers\ProviderController ;
  21. use Application\MVC\Controllers\InstallerController ;
  22. use Application\MVC\Controllers\UnloggedController ;
  23. use Application\MVC\Controllers\CategoryTaxController ;
  24.  
  25.  
  26. class CotaPreco extends Object {
  27.  
  28. public static $instance = null ;
  29. /**
  30. * Gerenciador de controladores da aplicação
  31. * @var ControllerManager
  32. */
  33. private $controllerManager = null ;
  34.  
  35. /**
  36. * Constroí a aplicação adicionando os handlers
  37. */
  38. public function __construct () {
  39. $this->controllerManager = ControllerManager::getInstance ( ) ;
  40. $this->controllerManager->addController ( new InstallerController ( ) ) ;
  41. $this->controllerManager->addController ( new UnloggedController ( ) ) ;
  42. $this->controllerManager->addController ( new HomeController ( ) ) ;
  43. $this->controllerManager->addController ( new LoginController ( ) ) ;
  44. $this->controllerManager->addController ( new ProviderController ( ) ) ;
  45. }
  46. /**
  47. * Delega a manipulação das requisições feitas à
  48. * aplicação ao controlador responsável.
  49. */
  50. public function handle ( ) {
  51. try {
  52. Registry::getInstance()->set ( 'ApplicationConfig' , new ApplicationConfig ( ) ) ;
  53. Registry::getInstance()->set ( 'Session' , SessionHandler::getInstance ( ApplicationDIR ) ) ;
  54. Registry::getInstance()->set ( 'isInstalled' , $this->isInstalled ( ) ) ;
  55. Registry::getInstance()->set ( 'RequestParser' , new RequestParser ( ) );
  56. Registry::getInstance()->set ( 'Cookie' , CookieHandler::getInstance ( ) ) ;
  57. if ( $this->isInstalled ( ) ) {
  58. Registry::getInstance()->set ( 'MySQL' , new MySQLConnection ( ) ) ;
  59. $session = Registry::getInstance ( )->get ( 'Session' ) ;
  60. if ( $session->get ( 'isLogged' ) === TRUE ) {
  61. $this->controllerManager->addController ( new CategoryController ( ) ) ;
  62. $this->controllerManager->addController ( new ProductController ( ) ) ;
  63. $this->controllerManager->addController ( new CategoryTaxController ( ) ) ;
  64. }
  65. }
  66. $this->controllerManager->handle ( ) ;
  67. } catch ( Exception $e ) {
  68. $view = new ErrorView ( ) ;
  69. $view->setErrorMessage ( $e->getMessage ( ) ) ;
  70. $view->display ( ) ;
  71. }
  72. }
  73. /**
  74. * Verifica se a aplicação está instalada
  75. * @return boolean
  76. */
  77. public function isInstalled () {
  78. $config = Registry::getInstance ()->get ( 'ApplicationConfig' ) ;
  79. return filter_var ( $config->getConfig ( 'misc' , 'installed' ) , FILTER_VALIDATE_BOOLEAN ) ;
  80. }
  81. /**
  82. * Recupera a instância da aplicação.
  83. * @return Application
  84. */
  85. public static function getInstance () {
  86. if ( self::$instance == null ) {
  87. self::$instance = new CotaPreco() ;
  88. }
  89. return self::$instance ;
  90. }
  91.  
  92. /**
  93. * Inicializa a aplicação.
  94. * @see Application::handle()
  95. */
  96. public static function initialize () {
  97. self::getInstance ()->handle () ;
  98. }
  99.  
  100. }
Add Comment
Please, Sign In to add comment