Completed
Push — master ( bf2930...494091 )
by David
07:08 queued 03:26
created

Adapters/CodeIgniter/libraries/Dwootemplate.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 40 and the first side effect is on line 18.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Copyright (c) 2013-2016
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Adapters\CodeIgniter\libraries
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2016 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.0
13
 * @date      2016-09-19
14
 * @link      http://dwoo.org/
15
 */
16
17
if (!defined('BASEPATH')) {
18
    exit('No direct script access allowed');
19
}
20
21
require 'dwoo/dwooAutoload.php';
22
23
/**
24
 * Creates an template interface from Codeigniter to DWOO.
25
 *
26
 * This software is provided 'as-is', without any express or implied warranty.
27
 * In no event will the authors be held liable for any damages arising from the use of this software.
28
 *
29
 * @author    Stefan Verstege <[email protected]>
30
 * @copyright Copyright (c) 2008, Stefan Verstege
31
 * @license   http://dwoo.org/LICENSE   Modified BSD License
32
 *
33
 * @link http://www.newmedia.nl/
34
 *
35
 * @version 1.1.0
36
 * @date    2009-07-18
37
 *
38
 * @uses the dwoo package from http://dwoo.org
39
 */
40
class Dwootemplate extends Dwoo_Core
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
41
{
42
    protected $dwoo_data = array();
43
44
    /**
45
     * Constructor for the DwooTemplate engine.
46
     */
47
    public function __construct()
48
    {
49
        // Call parents constructor
50
        parent::__construct();
51
52
        // Set the config settings
53
        $this->initialize();
54
55
        // Assign some defaults to dwoo
56
        $CI = get_instance();
57
        $this->dwoo_data = new Dwoo_Data();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Dwoo_Data() of type object<Dwoo_Data> is incompatible with the declared type array of property $dwoo_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        $this->dwoo_data->js_files = array();
59
        $this->dwoo_data->css_files = array();
60
        $this->dwoo_data->CI = $CI;
61
        $this->dwoo_data->site_url = $CI->config->site_url(); // so we can get the full path to CI easily
62
        $this->dwoo_data->uniqid = uniqid();
63
        $this->dwoo_data->timestamp = mktime();
64
65
        log_message('debug', 'Dwoo Template Class Initialized');
66
    }
67
68
    /**
69
     * Assign data to dwoo data object.
70
     *
71
     * @param string $key
72
     * @param mixed  $value
73
     */
74
    public function assign($key, $value)
75
    {
76
        $this->dwoo_data->$key = $value;
77
    }
78
79
    /**
80
     * Add Javascript files to template.
81
     *
82
     * @param string $js
83
     */
84
    public function add_js($js)
85
    {
86
        $current = $this->dwoo_data->js_files;
87
        $current[] = $js;
88
        $this->dwoo_data->js_files = $current;
89
    }
90
91
    /**
92
     * Add Css stylesheets to template.
93
     *
94
     * @param string $css
95
     */
96
    public function add_css($css)
97
    {
98
        $current = $this->dwoo_data->css_files;
99
        $current[] = $css;
100
        $this->dwoo_data->css_files = $current;
101
    }
102
103
    /**
104
     * Display or return the compiled template
105
     * Since we assign the results to the standard CI output module
106
     * you can also use the helper from CI in your templates!!
107
     *
108
     * @param string $sTemplate
109
     * @param bool   $return
110
     *
111
     * @return mixed
112
     */
113
    public function display($sTemplate, $return = false)
114
    {
115
        // Start benchmark
116
        $CI = get_instance();
117
        $CI->benchmark->mark('dwoo_parse_start');
118
119
        // Check if file exists
120
        if (!file_exists($this->template_dir.$sTemplate)) {
121
            $message = sprintf('Template file \'%s\' not found.', $sTemplate);
122
            show_error($message);
123
            log_message('error', $message);
124
        }
125
126
        // Create new template
127
        $tpl = new Dwoo_Template_File($this->template_dir.$sTemplate);
128
129
        // render the template
130
        $template = $this->get($tpl, $this->dwoo_data);
131
132
        // Finish benchmark
133
        $CI->benchmark->mark('dwoo_parse_end');
134
135
        // Return results or not ?
136
        if ($return == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
137
            $CI->output->final_output = $template;
138
        } else {
139
            return $template;
140
        }
141
    }
142
143
    /**
144
     * Toggle Codeigniter profiler on/off.
145
     */
146
    public function enable_profiler($toggle = true)
147
    {
148
        $CI = get_instance();
149
        $CI->output->enable_profiler($toggle);
150
    }
151
152
    /**
153
     * Set http header.
154
     *
155
     * @example $this->output->set_header("HTTP/1.1 200 OK");
156
     * @example $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
157
     *
158
     * @param string $header
159
     */
160
    public function set_header($header)
161
    {
162
        $CI = get_instance();
163
        $CI->output->set_header($header);
164
    }
165
166
    /**
167
     * Set status header.
168
     *
169
     * @example $this->output->set_status_header('401');
170
     * @example // Sets the header as: Unauthorized
171
     *
172
     * @param string $header
173
     */
174
    public function set_status_header($header)
175
    {
176
        $CI = get_instance();
177
        $CI->output->set_status_header($header);
178
    }
179
180
    /**
181
     * Assign the dwootemplate config items to the instance.
182
     */
183
    private function initialize()
184
    {
185
        $CI = get_instance();
186
        $CI->config->load('dwootemplate', true);
187
        $config = $CI->config->item('dwootemplate');
188
        foreach ($config as $key => $val) {
189
            $this->$key = $val;
190
        }
191
    }
192
}
193