|
1 | | -<?php |
2 | | - |
3 | | -// set up some constants first, because some of my friends are probably going |
4 | | -// to be setting this up in a manner that is "security conscious" - moving the |
5 | | -// app directory outside of this folder and into its parent somewhere |
6 | | -define('PHAR_MODE', class_exists('Phar') && Phar::running()); |
7 | | - |
8 | | -if (!defined('APP_DIR')) { |
9 | | - if (PHAR_MODE) { |
10 | | - define('APP_DIR', realpath(getcwd() . '/app')); |
11 | | - define('APP_VENDOR_DIR', 'phar://' . basename(__DIR__) . '/app/vendor'); |
12 | | - } else { |
13 | | - define('APP_DIR', realpath(__DIR__ . '/app')); |
14 | | - define('APP_VENDOR_DIR', realpath(APP_DIR . '/vendor')); |
15 | | - } |
16 | | -} |
17 | | - |
18 | | -// require composer |
19 | | -require APP_VENDOR_DIR . '/autoload.php'; |
20 | | - |
21 | | -use League\Container\Container; |
22 | | -use Psr\Http\Message\ResponseInterface as Response; |
23 | | -use Psr\Http\Message\ServerRequestInterface as Request; |
24 | | -use Slim\Exception\HttpNotFoundException; |
25 | | -use Slim\Factory\AppFactory; |
26 | | - |
27 | | -// set up service provider |
28 | | -$container = new Container(); |
29 | | -$container->addServiceProvider(App\ServiceProvider::class); |
30 | | - |
31 | | -// start messing about with slim |
32 | | -$app = AppFactory::createFromContainer($container); |
33 | | - |
34 | | -$app->any('/dl', function (Request $request, Response $response, array $arguments) { |
35 | | - $response->getBody()->write('85'); |
36 | | - return $response; |
37 | | -}); |
38 | | - |
39 | | -$app->any('/', function (Request $request, Response $response, array $arguments) { |
40 | | - return $response->withStatus(200); |
41 | | -}); |
42 | | - |
43 | | -$app->group('/api', App\Router\Api::class); |
44 | | -$app->group('/page', App\Router\Page::class); |
45 | | -$app->any('/{alias:[\w\.]*?}', new App\Router\FileRoute($app)); |
46 | | -$app->any('/{alias:[\w\.]*?}/{ext}', new App\Router\FormattedFileRoute($app)); |
47 | | - |
48 | | -$afterMiddleware = function ($request, $handler) { |
49 | | - try { |
50 | | - $response = $handler->handle($request); |
51 | | - } catch (UnexpectedValueException $exception) { |
52 | | - return (new Slim\Psr7\Response())->withStatus(404); |
53 | | - } catch (HttpNotFoundException $exception) { |
54 | | - return (new Slim\Psr7\Response())->withStatus(404); |
55 | | - } catch (Throwable $exception) { |
56 | | - $response = new Slim\Psr7\Response(); |
57 | | - $response->getBody()->write('-1'); |
58 | | - return $response->withStatus(500); |
59 | | - } |
60 | | - return $response; |
61 | | -}; |
62 | | - |
63 | | -$app->add($afterMiddleware); |
64 | | -$app->run(); |
| 1 | +<?php |
| 2 | + |
| 3 | +// set up some constants first, because some of my friends are probably going |
| 4 | +// to be setting this up in a manner that is "security conscious" - moving the |
| 5 | +// app directory outside of this folder and into its parent somewhere |
| 6 | +define('PHAR_MODE', class_exists('Phar') && Phar::running()); |
| 7 | + |
| 8 | +if (!defined('APP_DIR')) { |
| 9 | + if (PHAR_MODE) { |
| 10 | + define('APP_DIR', realpath(getcwd() . '/app')); |
| 11 | + define('APP_VENDOR_DIR', 'phar://' . basename(__DIR__) . '/app/vendor'); |
| 12 | + } else { |
| 13 | + define('APP_DIR', realpath(__DIR__ . '/app')); |
| 14 | + define('APP_VENDOR_DIR', realpath(APP_DIR . '/vendor')); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// require composer |
| 19 | +require APP_VENDOR_DIR . '/autoload.php'; |
| 20 | + |
| 21 | +use League\Container\Container; |
| 22 | +use Psr\Http\Message\ResponseInterface as Response; |
| 23 | +use Psr\Http\Message\ServerRequestInterface as Request; |
| 24 | +use Slim\Exception\HttpNotFoundException; |
| 25 | +use Slim\Factory\AppFactory; |
| 26 | + |
| 27 | +// set up service provider |
| 28 | +$container = new Container(); |
| 29 | +$container->addServiceProvider(App\ServiceProvider::class); |
| 30 | + |
| 31 | +// start messing about with slim |
| 32 | +$app = AppFactory::createFromContainer($container); |
| 33 | + |
| 34 | +$app->any('/dl', function (Request $request, Response $response, array $arguments) { |
| 35 | + $response->getBody()->write('85'); |
| 36 | + return $response; |
| 37 | +}); |
| 38 | + |
| 39 | +$app->any('/', function (Request $request, Response $response, array $arguments) { |
| 40 | + return $response->withStatus(200); |
| 41 | +}); |
| 42 | + |
| 43 | +$app->group('/api', App\Router\Api::class); |
| 44 | +$app->group('/page', App\Router\Page::class); |
| 45 | +$app->any('/{alias:[\w\.]*?}', new App\Router\FileRoute($app)); |
| 46 | +$app->any('/{alias:[\w\.]*?}/{ext}', new App\Router\FormattedFileRoute($app)); |
| 47 | + |
| 48 | +$afterMiddleware = function ($request, $handler) { |
| 49 | + try { |
| 50 | + $response = $handler->handle($request); |
| 51 | + } catch (UnexpectedValueException $exception) { |
| 52 | + return (new Slim\Psr7\Response())->withStatus(404); |
| 53 | + } catch (HttpNotFoundException $exception) { |
| 54 | + return (new Slim\Psr7\Response())->withStatus(404); |
| 55 | + } catch (Throwable $exception) { |
| 56 | + $response = new Slim\Psr7\Response(); |
| 57 | + $response->getBody()->write('-1'); |
| 58 | + return $response->withStatus(500); |
| 59 | + } |
| 60 | + return $response; |
| 61 | +}; |
| 62 | + |
| 63 | +$app->add($afterMiddleware); |
| 64 | +$app->run(); |
0 commit comments