-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
80 lines (66 loc) · 2.78 KB
/
plugin.php
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
<?php
declare(strict_types=1);
/**
* @link https://awilum.github.io/flextype
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype\Plugin\Site;
use Flextype\Plugin\Site\Console\Commands\Site\SiteGenerateCommand;
use Flextype\Plugin\Site\Console\Commands\Cache\CacheClearSiteStaticCommand;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Middlewares\TrailingSlash;
use function is_file;
use function Glowy\Strings\strings;
use function Flextype\console;
use function Flextype\app;
use function Flextype\registry;
use function Flextype\urlFor;
use function Flextype\getBasePath;
use function Flextype\getUriString;
/**
* Ensure vendor libraries exist
*/
! is_file($siteAutoload = __DIR__ . '/vendor/autoload.php') and exit('Please run: <i>composer install</i> for site plugin');
/**
* Register The Auto Loader
*
* Composer provides a convenient, automatically generated class loader for
* our application. We just need to utilize it! We'll simply require it
* into the script here so that we don't have to worry about manual
* loading any of our classes later on. It feels nice to relax.
* Register The Auto Loader
*/
$siteLoader = require_once $siteAutoload;
console()->add(new CacheClearSiteStaticCommand());
console()->add(new SiteGenerateCommand());
// Add middleware TrailingSlash for all routes
if (getUriString() !== strings(registry()->get('flextype.settings.base_path'))->prepend('/')->append('/')->toString()) {
app()->add((new TrailingSlash(registry()->get('plugins.site.settings.trailing_slash')))->redirect());
}
// Load routes
require_once __DIR__ . '/src/routes/web.php';
// Redirects
$redirects = registry()->get('plugins.site.settings.redirects');
if (count($redirects) > 0) {
foreach ($redirects as $redirect) {
app()->get($redirect['from'], function (ServerRequestInterface $request, ResponseInterface $response) use ($redirect) {
$to = '';
if (isset($redirect['to_route_name'])) {
$to = urlFor($redirect['to_route_name'], isset($redirect['data']) ? $redirect['data'] : [], isset($redirect['queryParams']) ? $redirect['queryParams'] : []);
}
if (isset($redirect['to'])) {
$to = getBasePath() . $redirect['to'];
}
if (isset($redirect['to_external'])) {
header('Location: ' . $redirect['to_external'], true, isset($redirect['status']) ? $redirect['status'] : 301);
exit(1);
}
$response = $response->withStatus(isset($redirect['status']) ? $redirect['status'] : 301);
$response = $response->withHeader('Location', $to);
return $response;
});
}
}