Completed
Push — master ( 128851...3506e7 )
by Vitaly
07:15
created

ExternalModule   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 13
Bugs 4 Features 2
Metric Value
wmc 16
c 13
b 4
f 2
lcom 1
cbo 2
dl 0
loc 138
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __sleep() 0 5 1
A __construct() 0 14 2
A copy() 0 13 1
B view() 0 28 5
A render() 0 14 4
A setId() 0 4 1
A prepare() 0 4 1
A init() 0 6 1
1
<?php
2
namespace samson\core;
3
4
use samsonframework\core\ResourcesInterface;
5
use samsonframework\core\SystemInterface;
6
use samsonframework\core\ViewInterface;
7
use samsonphp\event\Event;
8
9
/**
10
 * SamsonPHP external module
11
 *
12
 * @author Vitaly Iegorov <[email protected]>
13
 */
14
class ExternalModule extends Module implements iExternalModule
15
{
16
    /** @var Module Pointer to parent module */
17
    public $parent = null;
18
19
    /**
20
     * ExternalModule constructor.
21
     *
22
     * @param string $path
23
     * @param ResourcesInterface $resources
24
     * @param SystemInterface $system
25
     */
26
    public function __construct($path, ResourcesInterface $resources, SystemInterface $system)
27
    {
28
        // Module identifier not specified - set it to NameSpace\Classname
29
        if (!isset($this->id{0})) {
30
            // Generate identifier from module class
31
            $this->id = strtolower(str_replace(__NS_SEPARATOR__, '_', get_class($this)));
32
        }
33
34
        // Subscribe to an config ready core event
35
        Event::subscribe('core.started', array(& $this, 'init'));
36
37
        // Call parent constructor
38
        parent::__construct($this->id, $path, $resources, $system);
39
    }
40
41
    /** @see \samson\core\iExternalModule::copy() */
42
    public function &copy()
43
    {
44
        // Get current class name
45
        $classname = get_class($this);
46
47
        // Create copy instance
48
        $clone = new $classname($this->path, $this->resourceMap, $this->system);
49
        $clone->views = &$this->views;
50
        $clone->parent = &$this->parent;
51
        $clone->path = $this->path;
52
53
        return $clone;
54
    }
55
56
    /** Обработчик сериализации объекта */
57
    public function __sleep()
58
    {
59
        // Remove all unnecessary fields from serialization
60
        return array_diff(array_keys(get_object_vars($this)), array('view_path', 'view_html', 'view_data'));
61
    }
62
63
64
    /**
65
     * Set current view for rendering.
66
     *
67
     * @param string $viewPath Path for view searching
68
     * @return self Chaining
69
     */
70
    public function view($viewPath)
71
    {
72
        if (is_a($viewPath, ViewInterface::class)) {
73
            $this->view_path = $viewPath;
74
            return $this;
75
            // Old string approach - will be deprecated soon
76
        } elseif (is_string($viewPath)) {
77
            //elapsed('['.$this->id.'] Setting view context: ['.$viewPath.']');
78
            // Find full path to view file
79
            $this->view_path = $this->findView($viewPath);
80
81
            // If we have not found view in current module but we have parent module
82
            if (isset($this->parent) && $this->view_path === false) {
83
                //elapsed('['.$this->id.'] Cannot set view['.$viewPath.'] - passing it to parent['.$this->parent->id.']');
84
85
                /*
86
                 * Call parent module view setting and return PARENT module to chain
87
                 * actually switching current module in chain
88
                 */
89
                return $this->parent->view($viewPath);
90
            } else { // Call default module behaviour
91
                // Call default module behaviour
92
                parent::view($this->view_path);
93
94
                return $this;
95
            }
96
        }
97
    }
98
99
    /**
100
     * Overloading default module rendering behaviour
101
     * as it is used in templates and views using m()->render()
102
     * without specifying concrete module or passing a variable.
103
     *
104
     * @param string $controller Controller action name
105
     */
106
    public function render($controller = null)
107
    {
108
        // If we have parent module connection and have no view set
109
        if (isset($this->parent) && $this->view_path == false && !method_exists($this, $controller)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->view_path of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
110
            // Merge current and parent module view data
111
            $this->parent->view_data = array_merge($this->parent->view_data, $this->view_data);
112
            // Set internal view context data pointer
113
            $this->parent->data = &$this->parent->view_data[$this->parent->view_context];
114
            // Call parent render
115
            $this->parent->render($controller);
116
        } else { // Call default module behaviour
117
            parent::render($controller);
118
        }
119
    }
120
121
    public function setId($id)
122
    {
123
        $this->id = $id;
124
    }
125
126
    /**
127
     * Module preparation handler.
128
     * This function is triggered after module instance is being created.
129
     *
130
     * @return bool Preparation result
131
     */
132
    public function prepare()
133
    {
134
        return true;
135
    }
136
137
    /**
138
     * Module initialization.
139
     * This function is triggered when system has started. So here
140
     * we have all modules already prepared and loaded.
141
     *
142
     * @param array $params Collection of module initialization parameters
143
     * @return bool Initialization result
144
     */
145
    public function init(array $params = array())
146
    {
147
        $this->set($params);
148
149
        return true;
150
    }
151
}
152