Completed
Push — dev ( 1806ac )
by Stone
12s
created

Controller::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Core;
4
5
use Twig\Template;
6
7
/**
8
 * Class Controller
9
 * @package Core
10
 *
11
 * PHP version 7
12
 */
13
abstract class Controller
14
{
15
    /**
16
     * the data that will be pushed to the view
17
     * @var array
18
     */
19
    protected $data = [];
20
21
    /**
22
     * @var Container dependency injector
23
     */
24
    protected $container;
25
26
    /**
27
     * @var Dependency\Session the session handler
28
     */
29
    protected $session;
30
31
    /**
32
     * this will automaticly load all the modules listed and store them as $moduleName in tle class
33
     * Child classes can call aditional modules by calling $this->
34
     * @var array List of modules to load
35
     */
36
    protected $loadModules = [
37
        'Csrf',
38
        'AlertBox'
39
    ];
40
41
    /**
42
     * We need to declare out modules here, else they will be public
43
     * @var object
44
     */
45
    protected $csrf;
46
    protected $alertBox;
47
48
    /**
49
     * Controller constructor.
50
     * @param Container $container
51
     *
52
     * @throws \ErrorException
53
     */
54
    public function __construct(Container $container)
55
    {
56
        $this->container = $container;
57
58
        //removing any duplicates that could have been passed in child classes
59
        $this->loadModules = array_unique($this->loadModules);
60
61
        //We load all our module objects into our object
62
        foreach ($this->loadModules as $loadModule) {
63
            $loadModuleObj = 'Core\\Modules\\' . $loadModule;
64
65
            $loadModuleName = lcfirst($loadModule);
66
            $loadedModule = new $loadModuleObj($this->container);
67
            //Modules must be children of the Module template
68
            if (!is_subclass_of($loadedModule, 'Core\Modules\Module')) {
69
                throw new \ErrorException('Modules musit be a sub class of module');
70
            }
71
72
            //we are not allowed to create public modules, they must be a placeholder ready
73
            if (!property_exists($this, $loadModuleName)) {
74
                throw new \ErrorException('class var ' . $loadModuleName . ' not present');
75
            }
76
            $this->$loadModuleName = $loadedModule;
77
        }
78
        $this->session = $this->container->getSession();
79
80
        //Setting up csrf token security for all calls
81
        $this->data['csrf_token'] = $this->csrf->getCsrfKey(); //storing the security id into the data array to be sent to the view and added in the meta head
82
    }
83
84
    public function index()
85
    {
86
        //if no index, then redirect to the home page or throw an error if in dev; just for debugging purposes
87
        if (Config::DEV_ENVIRONMENT) {
88
            throw new \ErrorException("no index() available in controller call");
89
        }
90
        $this->container->getResponse()->redirect();
91
    }
92
93
    /**
94
     * Calls the templating engine and returns the rendered view
95
     *
96
     * @param $template string the template file name. .twig will be appended
97
     * @return string the rendered template
98
     * @throws \Twig_Error_Loader
99
     * @throws \Twig_Error_Runtime
100
     * @throws \Twig_Error_Syntax
101
     */
102
    public function getView($template)
103
    {
104
        $twig = $this->container->getTemplate();
105
        return $twig->render($template . '.twig', $this->data);
106
    }
107
108
    /**
109
     * rendering the view
110
     *
111
     * @param $template string the template file
112
     * @throws \Twig_Error_Loader
113
     * @throws \Twig_Error_Runtime
114
     * @throws \Twig_Error_Syntax
115
     */
116
    public function renderView($template): void
117
    {
118
        //checking if any alerts and pas the to the template
119
        if ($this->alertBox->alertsPending()) {
120
            $this->data['alert_messages'] = $this->alertBox->getAlerts();
121
        }
122
        if (Config::DEV_ENVIRONMENT) {
123
            $this->devHelper();
124
        }
125
        $twig = $this->container->getTemplate();
126
        $twig->display($template . '.twig', $this->data);
127
    }
128
129
    protected function devHelper($var = '')
130
    {
131
        if (!isset($this->data['dev_info'])) {
132
            $this->data['dev'] = true;
133
            $classMethods = [];
134
            if ($var != '') {
135
                $classMethods['passed_var'] = $var;
136
            }
137
            $classMethods['class_object_methods'] = get_class_methods(get_class($this));
138
            $classMethods['class_object_vars'] = get_object_vars($this);
139
            $classMethods['session_vars'] = $_SESSION;
140
141
            $this->data['dev_info'] = $classMethods;
142
        }
143
144
    }
145
}