Issues (1268)

src/RequestBootstrap.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace RssApp;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Annotations\AnnotationRegistry;
9
use Exception;
10
use Locale;
11
use RssApp\Components\Registry;
12
use RssApp\Components\Twig\Filters;
13
use RssApp\Components\Twig\Functions;
14
use Symfony\Bridge\Twig\Extension\TranslationExtension;
15
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
19
use Symfony\Component\Routing\RequestContext;
20
use Symfony\Component\Routing\Router;
21
use Symfony\Component\Translation\Loader\MoFileLoader;
22
use Symfony\Component\Translation\Translator;
23
use Twig\Environment;
24
use Twig\Loader\FilesystemLoader;
25
use Zend\Diactoros\ServerRequestFactory;
26
27
/**
28
 * @internal Intended to be a final abstract (or static) class (https://wiki.php.net/rfc/abstract_final_class)
29
 */
30
abstract class RequestBootstrap
31
{
32
33
    /**
34
     * Runs the class methods in order of declaration
35
     *
36
     * @param array $omitMethods
37
     *
38
     * @throws Exception
39
     */
40
    public static function initialize($omitMethods = []): void
41
    {
42
        $methods = get_class_methods(self::class);
43
        $key = array_search('initialize', $methods);
44
        if ($key !== false) {
45
            unset($methods[$key]);
46
        }
47
        foreach ($omitMethods as $omitMethod) {
48
            $omitKey = array_search($omitMethod, $methods);
49
            if ($omitKey !== false) {
50
                unset($methods[$key]);
51
            }
52
        }
53
        foreach ($methods as $method) {
54
            if (self::{$method}() === false) {
55
                throw new Exception($method.' failed to initialize');
56
            }
57
        }
58
    }
59
60
    protected static function router(): bool
61
    {
62
        $request = Request::createFromGlobals();
63
        $requestContext = new RequestContext();
64
        $requestContext->fromRequest($request);
65
        $logger = Registry::get('log');
66
        $fileLocator = new FileLocator([
67
            BASEPATH.DS.'src'.DS.'Controller',
68
        ]);
69
        $reader = new AnnotationReader();
70
        $annotatedLoader = new AnnotatedRouteControllerLoader($reader);
71
        $loader = new AnnotationDirectoryLoader($fileLocator, $annotatedLoader);
72
        $router = new Router($loader, BASEPATH.DS.'src'.DS.'Controller', [], $requestContext, $logger);
73
        $loader = require BASEPATH.DS.'external'.DS.'autoload.php';
74
        AnnotationRegistry::registerLoader([$loader, 'loadClass']);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

74
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader([$loader, 'loadClass']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
75
        Registry::set('router', $router);
76
        return true;
77
    }
78
79
    protected static function twig(): bool
80
    {
81
        $twig = Registry::get('twig');
82
        $twig->addGlobal('request', Registry::get('request'));
83
84
        return true;
85
    }
86
87
    protected static function twigTranslations(): bool
88
    {
89
        $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']) ?? getenv('DEFAULT_LANGUAGE');
90
        $messagesFile = BASEPATH.DS.'locale'.DS.$locale.DS.'LC_MESSAGES'.DS.'messages.mo';
91
92
        $twig = Registry::get('twig');
93
        $translator = new Translator($locale);
94
95
        if (is_file($messagesFile)) {
96
            $translator->addLoader('moloader', new MoFileLoader());
97
            $translator->addResource(
98
                'moloader',
99
                BASEPATH.DS.'locale'.DS.$locale.DS.'LC_MESSAGES'.DS.'messages.mo',
100
                $locale
101
            );
102
        }
103
104
        $translationExt = new TranslationExtension($translator);
105
        $twig->addExtension($translationExt);
106
        Registry::set('trans', $translationExt);
107
108
        return true;
109
    }
110
111
}
112