Blame view

public/index.php 2.65 KB
15479603   Alex Savenko   initialize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  <?php
  
  /** @var \Phalcon\Config $config */
  $config = null;
  
  /** @var \PhalconRest\Api $app */
  $app = null;
  
  /** @var \PhalconApi\Http\Response $response */
  $response = null;
  
  try {
  
      define("ROOT_DIR", __DIR__ . '/..');
      define("APP_DIR", ROOT_DIR . '/app');
      define("VENDOR_DIR", ROOT_DIR . '/vendor');
      define("CONFIG_DIR", APP_DIR . '/configs');
  
      define('APPLICATION_ENV', getenv('APPLICATION_ENV') ?: 'development');
  
      // Autoload dependencies
      require VENDOR_DIR . '/autoload.php';
  
      $loader = new \Phalcon\Loader();
  
      $loader->registerNamespaces([
          'App' => APP_DIR . '/library/App'
      ]);
  
      $loader->registerDirs([
          APP_DIR . '/views/'
      ]);
  
      $loader->register();
  
      // Config
      $configPath = CONFIG_DIR . '/default.php';
  
      if (!is_readable($configPath)) {
          throw new Exception('Unable to read config from ' . $configPath);
      }
  
      $config = new Phalcon\Config(include_once $configPath);
  
      $envConfigPath = CONFIG_DIR . '/server.' . APPLICATION_ENV . '.php';
  
      if (!is_readable($envConfigPath)) {
          throw new Exception('Unable to read config from ' . $envConfigPath);
      }
  
      $override = new Phalcon\Config(include_once $envConfigPath);
  
      $config = $config->merge($override);
  
  
      // Instantiate application & DI
      $di = new PhalconRest\Di\FactoryDefault();
      $app = new PhalconRest\Api($di);
  
      // Bootstrap components
      $bootstrap = new App\Bootstrap(
          new App\Bootstrap\ServiceBootstrap,
          new App\Bootstrap\MiddlewareBootstrap,
          new App\Bootstrap\CollectionBootstrap,
          new App\Bootstrap\RouteBootstrap,
          new App\Bootstrap\AclBootstrap
      );
  
      $bootstrap->run($app, $di, $config);
  
      // Start application
      $app->handle();
  
      // Set appropriate response value
      $response = $app->di->getShared(App\Constants\Services::RESPONSE);
  
      $returnedValue = $app->getReturnedValue();
  
      if($returnedValue !== null) {
  
          if (is_string($returnedValue)) {
              $response->setContent($returnedValue);
          } else {
              $response->setJsonContent($returnedValue);
          }
      }
  
  } catch (\Exception $e) {
  
      // Handle exceptions
      $di = $app && $app->di ? $app->di : new PhalconRest\Di\FactoryDefault();
      $response = $di->getShared(App\Constants\Services::RESPONSE);
      if(!$response || !$response instanceof PhalconApi\Http\Response){
          $response = new PhalconApi\Http\Response();
      }
  
      $debugMode = isset($config->debug) ? $config->debug : (APPLICATION_ENV == 'development');
  
      $response->setErrorContent($e, $debugMode);
  }
  finally {
  
      // Send response
      if (!$response->isSent()) {
          $response->send();
      }
  }