application.services.php
1 year ago
command.bus.php
2 years ago
container.php
1 year ago
domain.event.bus.php
7 years ago
domain.services.php
2 years ago
infrastructure.services.php
1 year ago
infrastructure.user.php
7 years ago
repositories.php
2 years ago
request.php
1 year ago
container.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 4 | use AmeliaBooking\Infrastructure\Common\Container; |
| 5 | use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage; |
| 6 | |
| 7 | // Handle the 404 API calls |
| 8 | $entries['notFoundHandler'] = function () { |
| 9 | return function ($request, \Slim\Http\Response $response) { |
| 10 | return $response->withStatus(404); |
| 11 | }; |
| 12 | }; |
| 13 | |
| 14 | // Handle the Method Not Allowed API calls |
| 15 | $entries['notAllowedHandler'] = function () { |
| 16 | return function ($request, \Slim\Http\Response $response) { |
| 17 | return $response->withStatus(405); |
| 18 | }; |
| 19 | }; |
| 20 | |
| 21 | // Handle the errors |
| 22 | $entries['errorHandler'] = function (Container $c) { |
| 23 | return function ($request, \Slim\Http\Response $response, $exception) use ($c) { |
| 24 | /** @var Exception $exception */ |
| 25 | |
| 26 | switch (get_class($exception)) { |
| 27 | case \AmeliaBooking\Application\Common\Exceptions\AccessDeniedException::class: |
| 28 | $status = \AmeliaBooking\Application\Controller\Controller::STATUS_FORBIDDEN; |
| 29 | break; |
| 30 | default: |
| 31 | $status = \AmeliaBooking\Application\Controller\Controller::STATUS_INTERNAL_SERVER_ERROR; |
| 32 | } |
| 33 | |
| 34 | $responseMessage = ['message' => $exception->getMessage()]; |
| 35 | |
| 36 | if (method_exists($request, 'getParam') && $request->getParam('showAmeliaSqlExceptions')) { |
| 37 | $responseMessage['exception'] = $exception->getPrevious() ? $exception->getPrevious()->getMessage() : ''; |
| 38 | } |
| 39 | |
| 40 | return $response->withStatus($status) |
| 41 | ->withHeader('Content-Type', 'text/html') |
| 42 | ->write(json_encode($responseMessage)); |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | // Disabled for now for easier debug |
| 48 | //// Handle PHP errors |
| 49 | //$entries['phpErrorHandler'] = function (Container $c) { |
| 50 | // return function ($request, \Slim\Http\Response $response, $exception) use ($c) { |
| 51 | // /** @var Exception $exception */ |
| 52 | // |
| 53 | // return $response->withStatus(500) |
| 54 | // ->withHeader('Content-Type', 'text/html') |
| 55 | // ->write($exception->getMessage()); |
| 56 | // }; |
| 57 | //}; |
| 58 | |
| 59 | ########################################################################## |
| 60 | ########################################################################## |
| 61 | # App common |
| 62 | ########################################################################## |
| 63 | ########################################################################## |
| 64 | |
| 65 | // |
| 66 | $entries['app.connection'] = function () { |
| 67 | $config = new \AmeliaBooking\Infrastructure\WP\config\Database(); |
| 68 | |
| 69 | $settingsService = new SettingsService(new SettingsStorage()); |
| 70 | |
| 71 | $mysqliEnabled = $settingsService->getSetting('db', 'mysqliEnabled'); |
| 72 | |
| 73 | $dbSettingsPort = $settingsService->getSetting('db', 'port'); |
| 74 | |
| 75 | $port = !empty($dbSettingsPort) ? $dbSettingsPort : 3306; |
| 76 | |
| 77 | if (!extension_loaded('pdo_mysql') || $mysqliEnabled) { |
| 78 | $config::$connection = $config::$connection ?: new \AmeliaBooking\Infrastructure\DB\MySQLi\Connection( |
| 79 | $config('host'), |
| 80 | $config('database'), |
| 81 | $config('username'), |
| 82 | $config('password'), |
| 83 | $config('charset'), |
| 84 | $port |
| 85 | ); |
| 86 | |
| 87 | return $config::$connection; |
| 88 | } |
| 89 | |
| 90 | $config::$connection = $config::$connection ?: new \AmeliaBooking\Infrastructure\DB\PDO\Connection( |
| 91 | $config('host'), |
| 92 | $config('database'), |
| 93 | $config('username'), |
| 94 | $config('password'), |
| 95 | $config('charset'), |
| 96 | $port |
| 97 | ); |
| 98 | |
| 99 | return $config::$connection; |
| 100 | }; |
| 101 | |
| 102 | ################ |
| 103 | # Repositories # |
| 104 | ################ |
| 105 | require 'repositories.php'; |
| 106 | |
| 107 | ############################ |
| 108 | # Currently logged in user # |
| 109 | ############################ |
| 110 | require 'infrastructure.user.php'; |
| 111 | |
| 112 | ################### |
| 113 | # Domain Services # |
| 114 | ################### |
| 115 | require 'domain.services.php'; |
| 116 | |
| 117 | ######################## |
| 118 | # Application Services # |
| 119 | ######################## |
| 120 | require 'application.services.php'; |
| 121 | |
| 122 | ######################## |
| 123 | # Infrastructure Services # |
| 124 | ######################## |
| 125 | require 'infrastructure.services.php'; |
| 126 | |
| 127 | ############### |
| 128 | # Command bus # |
| 129 | ############### |
| 130 | require 'command.bus.php'; |
| 131 | |
| 132 | #################### |
| 133 | # Domain event bus # |
| 134 | #################### |
| 135 | require 'domain.event.bus.php'; |
| 136 | |
| 137 | $entries['settings'] = [ |
| 138 | // Slim Settings |
| 139 | 'determineRouteBeforeAppMiddleware' => true, |
| 140 | 'displayErrorDetails' => true, |
| 141 | 'addContentLengthHeader' => false, //added due to error on dev server (check the cause) |
| 142 | ]; |
| 143 | |
| 144 | ###################### |
| 145 | # Request overriding # |
| 146 | ###################### |
| 147 | require 'request.php'; |
| 148 | |
| 149 | return new Container($entries); |
| 150 |