View::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
use Pug\Pug as Pug;
6
7
class View
8
{
9
    /**
10
     * Application Object
11
     *
12
     * @var \System\Application
13
     */
14
    private $app;
15
16
    /**
17
     * Constructor
18
     *
19
     * @param \System\Application $app
20
     */
21
    public function __construct(Application $app)
22
    {
23
        $this->app = $app;
24
    }
25
26
    /**
27
     * Render the given path with the passed
28
     * variables and generate new view object for it
29
     *
30
     * @property object $url
31
     * @property object $file
32
     * @param string $path
33
     * @param array $context
34
     * @return string
35
     */
36
    public function render(string $path, array $context)
37
    {
38
        $pug = new Pug(array(
39
            'pretty' => true,
40
            'cache' => ($_ENV['APP_DEBUG'] == 'true') ? false : 'template' . DIRECTORY_SEPARATOR . 'cache',
41
            'basedir' => 'template',
42
            'upToDateCheck' => false,
43
        ));
44
45
        $host = $this->app->url->host();
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
        $dir = $this->app->file->root();
0 ignored issues
show
Documentation introduced by
The property file does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
        $parameters = $this->parameters();
48
49
        $file = $this->filePath($path, $dir);
50
51
        $public = $this->_public($dir, $host);
52
        $page = strtolower($this->app->route->getPage());
0 ignored issues
show
Documentation introduced by
The property route does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
        $title = $this->app->msg->$page('title');
0 ignored issues
show
Documentation introduced by
The property msg does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
55
        $context += [
56
            'lang' => $_ENV['APP_LANG'],
57
            'charset' => $_ENV['APP_CHARSET'],
58
            'decsription' => $_ENV['APP_DECSRIPTION'],
59
            'keywords' => $_ENV['APP_KEYWORDS'],
60
            'auth' => $_ENV['APP_AUTH'],
61
            'host' => $host,
62
            'parameters' => $parameters,
63
            '_public' => $public,
64
            'title' => $title,
65
            'page' => $page,
66
        ];
67
        return $pug->render($file, $context);
68
    }
69
70
    /**
71
     * Generate the path
72
     *
73
     * @param string $path
74
     * @param string $dir
75
     * @return string
76
     */
77
    private function filePath($path, $dir)
78
    {
79
        $file = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
80
81
        $file = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $file . '.pug';
82
83
        return $file;
84
    }
85
86
    /**
87
     * Generate link to public
88
     *
89
     * @param string $dir
90
     * @param array $host
91
     * @return string
92
     */
93
    private function _public($dir, $host)
94
    {
95
        $public = assets();
96
97
        $public = str_replace($dir, $host, $public);
98
99
        $public = str_replace('\\', '/', $public);
100
101
        if (substr($public, -1) !== '/') {
102
            $public = $public . '/';
103
        }
104
        return $public;
105
    }
106
107
    /**
108
     * Get the parameters from the url
109
     * loop over all and give it the right link for each one
110
     * e.g. if the current page is localhost/admin/users
111
     * the right link for localhost is localhost or just /
112
     * the right link for admin is localhost/admin
113
     * the right link for users is localhost/admin/users
114
     *
115
     * @property object $url
116
     * @return array
117
     */
118
    private function parameters()
119
    {
120
        $url = $this->app->url->parameters();
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121
122
        if ($url === '/') {
123
            return;
124
        }
125
        $parameters = explode('/', $url);
126
127
        array_shift($parameters);
128
129
        $results = [];
130
131
        foreach ($parameters as $parameter) {
132
            $name = str_replace('-', ' ', $parameter);
133
134
            $length = strpos($url, $parameter) + strlen($parameter);
135
136
            $link = substr($url, 0, $length);
137
138
            $results[] = [
139
                'name' => $name,
140
                'link' => $link,
141
            ];
142
        }
143
        return $results;
144
    }
145
}
146