Completed
Push — master ( c27c49...e00954 )
by Nikita
07:15 queued 02:50
created

ExternalModule::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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