Passed
Pull Request — master (#43)
by Arman
03:54
created

HookDefaults::updateDebuggerStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.5.0
13
 */
14
15
namespace Quantum\Hooks;
16
17
use Quantum\Libraries\Database\Database;
0 ignored issues
show
Bug introduced by
The type Quantum\Libraries\Database\Database was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Quantum\Exceptions\RouteException;
19
use Quantum\Routes\RouteController;
20
use Twig\Loader\FilesystemLoader;
21
use Quantum\Debugger\Debugger;
22
use Quantum\Http\Response;
23
use Twig\TwigFunction;
24
25
/**
26
 * Class HookDefaults
27
 * @package Quantum\Hooks
28
 */
29
class HookDefaults implements HookInterface
30
{
31
32
    /**
33
     * Handle Headers
34
     * Allows Cross domain requests
35
     */
36
    public static function handleHeaders()
37
    {
38
        Response::setHeader('Access-Control-Allow-Origin', '*');
39
        Response::setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
40
        Response::setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
41
    }
42
43
    /**
44
     * Page not found
45
     * @throws \Quantum\Exceptions\RouteException
46
     */
47
    public static function pageNotFound()
48
    {
49
        throw RouteException::notFound();
50
    }
51
52
    /**
53
     * Template renderer
54
     * @param array $data
55
     * @return string
56
     * @throws \Twig\Error\LoaderError
57
     * @throws \Twig\Error\RuntimeError
58
     * @throws \Twig\Error\SyntaxError
59
     */
60
    public static function templateRenderer(array $data): string
61
    {
62
        $loader = new FilesystemLoader(modules_dir() . DS . current_module() . DS . 'Views');
63
        $twig = new \Twig\Environment($loader, $data['configs']);
64
65
        $definedFunctions = get_defined_functions();
66
67
        $allDefinedFuncitons = array_merge($definedFunctions['internal'], $definedFunctions['user']);
68
69
        foreach ($allDefinedFuncitons as $function) {
70
            if (function_exists($function)) {
71
                $twig->addFunction(new TwigFunction($function, $function));
72
            }
73
        }
74
75
        return $twig->render($data['view'] . '.php', $data['params']);
76
    }
77
78
    /**
79
     * Updates debugger store
80
     * @param array $data
81
     */
82
    public static function updateDebuggerStore(array $data)
83
    {
84
        $currentRoute = RouteController::getCurrentRoute();
85
86
        $routeInfo  = [];
87
88
        array_walk($currentRoute, function ($value, $key) use (&$routeInfo) {
89
            $routeInfo[ucfirst($key)] = $value;
90
        });
91
92
        $routeInfo['View'] = current_module() . DS . 'Views' . DS . $data['view'];
93
94
        Debugger::addToStore(Debugger::ROUTES, 'info', $routeInfo);
95
        Debugger::addToStore(Debugger::QUERIES, 'info', Database::queryLog());
96
    }
97
98
}
99