Passed
Push — master ( 99530c...78eac0 )
by Luka
04:00
created

functions.php ➔ set_flash()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 8
nop 1
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Helper functions for SampleApp
5
 *
6
 * @author    USAMI Kenta <[email protected]>
7
 * @copyright 2017 Baguette HQ
8
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
9
 */
10
11
use Monolog\Logger;
12
use Monolog\Handler\ChromePHPHandler;
13
use Monolog\Handler\NoopHandler;
14
use Monolog\Handler\StreamHandler;
15
use Teto\Routing\Router;
16
17
/**
18
 * @param  string $input
19
 * @return string
20
 */
21
function h($input)
22
{
23
    return htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
24
}
25
26
/**
27
 * @param string $tpl_name
28
 * @param array $data
29
 * @return string
30
 */
31
function view($tpl_name, array $data = [])
32
{
33
    $main_tpl = __DIR__ . "/../view/{$tpl_name}.tpl.php";
34
    if (!file_exists($main_tpl)) {
35
        throw new \RuntimeException("Template file not exists: {$tpl_name}");
36
    }
37
38
    ob_start();
39
    $var = new variables($data);
40
    include __DIR__ . '/../view/body.tpl.php';
41
42
    return ob_get_clean();
43
}
44
45
/**
46
 * @param  Router $router
47
 * @return Router
48
 */
49
function router(Router $router = null)
50
{
51
    /** @var Router $cache */
52
    static $cache;
53
54
    if ($router !== null) {
55
        $cache = $router;
56
    }
57
58
    return $cache;
59
}
60
61
/**
62
 * @param  array $input
63
 * @return void
64
 */
65
function set_flash(array $input)
0 ignored issues
show
Coding Style introduced by
set_flash uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
66
{
67
    $has_flash = !isset($_SESSION['_flash']) || !is_array($_SESSION['_flash']);
68
    $flash = $has_flash ? $_SESSION['_flash'] : [];
69
70
    foreach ($input as $key => $item) {
71
        $flash[$key] = filter_var($item, FILTER_DEFAULT);
72
    }
73
74
    $_SESSION['_flash'] = $flash;
75
}
76
77
/**
78
 * @param array  $flash
79
 * @return array
80
 */
81
function last_flash(array $flash = null)
82
{
83
    /** @var array $last_flash */
84
    static $last_flash = [];
85
86
    if ($flash !== null) {
87
        $last_flash = $flash;
88
    }
89
90
    return $last_flash;
91
}
92
93
/**
94
 * @return \Monolog\Logger
95
 */
96
function app_log()
97
{
98
    /** @var \Monolog\Logger */
99
    static $logger;
100
101
    if ($logger === null) {
102
        $path = implode(DIRECTORY_SEPARATOR, [__DIR__, getenv('MY_PHP_ENV') . '.php']);
103
        $fp = fopen($path, 'wb+');
104
        $logger = new \Monolog\Logger('');
105
        $logger->pushHandler(new StreamHandler($fp, Logger::INFO, true, null, true));
106
    }
107
108
    return $logger;
109
}
110
111
/**
112
 * @return \Monolog\Logger
113
 */
114
function chrome_log()
115
{
116
    /** @var \Monolog\Logger */
117
    static $logger;
118
119
    if ($logger === null) {
120
        $logger  = new \Monolog\Logger('');
121
        $handler = is_production() ? new NoopHandler : new ChromePHPHandler(Logger::INFO);
122
        $logger->pushHandler($handler);
123
    }
124
125
    return $logger;
126
}
127
128
/**
129
 * @return bool
130
 */
131
function is_production()
132
{
133
     return getenv('MY_PHP_ENV') === 'production';
134
}
135