Completed
Push — master ( ab4f53...e0a3aa )
by Vitaly
03:11
created

Module::viewHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 21.02.16 at 14:14
5
 */
6
namespace samsonphp\view;
7
8
use samsonframework\core\ResourcesInterface;
9
use samsonframework\core\SystemInterface;
10
use samsonframework\view\Generator;
11
12
/**
13
 * SamsonPHP view module
14
 * @package samsonphp\view
15
 */
16
class Module extends \samson\core\ExternalModule implements \samsonframework\core\CompressInterface
17
{
18
    /** View handling event */
19
    const EVENT_VIEW_HANDLER = 'samsonphp.view.handler';
20
    /** @var string Module identifier */
21
    protected $id = STATIC_RESOURCE_HANDLER;
22
    /** @var Generator */
23
    protected $generator;
24
25
    /**
26
     * Module constructor.
27
     *
28
     * @param string             $path
29
     * @param ResourcesInterface $resources
30
     * @param SystemInterface    $system
31
     * @param Generator          $generator
32
     */
33
    public function __construct($path, ResourcesInterface $resources, SystemInterface $system, Generator $generator = null)
34
    {
35
        parent::__construct($path, $resources, $system);
36
37
        $this->generator = isset($generator)
38
            ? $generator
39
            : new Generator(
40
                new \samsonphp\generator\Generator(),
41
                'view',
42
                array('\www', '\view'),
43
                View::class,
44
                array($this, 'viewHandler')
45
            );
46
47
        // Register View class file autoloader
48
        spl_autoload_register(array($this, 'autoload'));
49
    }
50
51
    /**
52
     * This method should be used to override generic compression logic.
53
     *
54
     * @param mixed $obj Pointer to compressor instance
55
     * @param array|null $code Collection of already compressed code
56
     *
57
*@return bool False if generic compression needs to be avoided
58
     */
59
    public function beforeCompress(&$obj = null, array &$code = null)
60
    {
61
62
    }
63
64
    /**
65
     * This method is called after generic compression logic has finished.
66
     *
67
     * @param mixed      $obj  Pointer to compressor instance
68
     * @param array|null $code Collection of already compressed code
69
     *
70
     * @return bool False if generic compression needs to be avoided
71
     */
72
    public function afterCompress(&$obj = null, array &$code = null)
73
    {
74
        $this->generator->generate($this->cache_path);
75
        // Iterate through generated php code
76
        foreach ($this->generator->metadata as $file => $metadata) {
77
            // Compress generated php code
78
            $obj->compress_php($metadata->generatedPath, $this, $code, $metadata->namespace);
79
        }
80
    }
81
82
    /**
83
     * Generator view code handler.
84
     *
85
     * @param string $viewCode Source view code
86
     *
87
     * @return string Modified view code
88
     */
89
    public function viewHandler($viewCode)
90
    {
91
        // Fire event
92
        \samsonphp\Event\Event::fire(self::EVENT_VIEW_HANDLER, array(&$viewCode));
93
94
        // Return modified view code
95
        return $viewCode;
96
    }
97
98
    /**
99
     * Help autoloading view classes as we know where we store them.
100
     *
101
     * @param string $class View class name for searching
102
     */
103
    public function autoload($class)
104
    {
105
        $classPath = $this->cache_path.str_replace('\\', '/', $class).'.php';
106
        if (file_exists($classPath)) {
107
            require_once($classPath);
108
        }
109
    }
110
111
    /**
112
     * Module preparation stage.
113
     * This function called after module instance creation but before
114
     * initialization stage.
115
     *
116
     * @param array $params Preparation stage parameters
117
     *
118
     * @return bool|void Preparation stage result
119
     */
120
    public function prepare(array $params = array())
121
    {
122
        $this->generator->scan(__SAMSON_CWD__.'/src');
123
        //$this->generator->scan(__SAMSON_CWD__.'/app');
124
        $signature = $this->generator->hash();
125
        if ($this->cache_refresh($signature)) {
126
            $this->generator->generate($this->cache_path);
127
            // Store cache file
128
            file_put_contents($signature, '');
129
        }
130
131
        // Add system static variable to all classes
132
        require_once 'View.php';
133
        View::$system = &$this->system;
134
135
        // Continue parent logic
136
        return parent::prepare($params);
0 ignored issues
show
Unused Code introduced by
The call to ExternalModule::prepare() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
137
    }
138
}
139