Completed
Push — master ( 3a82d8...63390c )
by Vitaly
02:07
created

Application::isModuleDependent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
namespace samsoncms\cms;
3
4
use samson\core\CompressableExternalModule;
5
use samsonphp\event\Event;
6
use samsonphp\router\Module;
7
8
/**
9
 * SamsonCMS external compressible application for integrating
10
 * @author Vitaly Iegorov <[email protected]>
11
 */
12
class Application extends CompressableExternalModule
13
{
14
    const EVENT_IS_CMS = 'samsonsms.is.cms';
15
16
    /** @var string Module identifier */
17
    public $id = 'cms';
18
19
    /** @var bool Flag that currently we are woring in SamsonCMS */
20
    protected $isCMS = false;
21
22
    public function init(array $params = array())
23
    {
24
        //trace('cmsInit');
25
        // Old applications main page rendering
26
        Event::subscribe('template.main.rendered', array($this, 'oldMainRenderer'));
27
        // Old applications menu rendering
28
        Event::subscribe('template.menu.rendered', array($this, 'oldMenuRenderer'));
29
30
        Event::subscribe('samson.url.build', array($this, 'buildUrl'));
31
32
        Event::subscribe('samson.url.args.created', array($this, 'parseUrl'));
33
34
        Event::subscribe(Module::EVENT_ROUTE_FOUND, array($this, 'activeModuleHandler'));
35
36
        Event::subscribe('samsonphp.router.create.module.routes', array($this, 'updateCMSPrefix'));
37
38
        //[PHPCOMPRESSOR(remove,start)]
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
        $moduleList   = $this->system->module_stack;
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\cms\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...
40
        foreach ($this->system->module_stack as $id => $module) {
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\cms\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...
41
            if (!$this->isModuleDependent($module) && $id != 'core' && !$this->ifModuleRelated($module)) {
42
                unset($moduleList[$id]);
43
            }
44
        }
45
46
        // Generate resources for new module
47
        $this->system->module('resourcer')->generateResources($moduleList, $this->path() . 'app/view/index.php');
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\cms\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...
48
        //[PHPCOMPRESSOR(remove,end)]
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
50
        // Call parent initialization
51
        return parent::init($params);
52
    }
53
54
    /**
55
     * If module is dependent from current module through composer.json.
56
     *
57
     * @param $module Module for checking
58
     * @return bool True if module dependent
59
     */
60
    protected function isModuleDependent($module)
61
    {
62
        return isset($module->composerParameters['composerName']) && in_array($module->composerParameters['composerName'], $this->composerParameters['required']);
0 ignored issues
show
Documentation introduced by
The property composerParameters does not exist on object<samsoncms\cms\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...
63
    }
64
65
    public function isCMS()
66
    {
67
        return $this->isCMS;
68
    }
69
70
    public function activeModuleHandler($module)
71
    {
72
        // Define if routed module is related to SamsonCMS
73
        if($this->isCMS = $this->ifModuleRelated($module)){
74
            // TODO: This should be removed - Reparse url
75
            url()->parse();
0 ignored issues
show
Bug introduced by
The method parse() cannot be called from this context as it is declared private in class samson\core\URL.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
76
77
            // Switch template to SamsonCMS
78
            $this->system->template($this->path() . 'app/view/index.php', true);
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\cms\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...
79
80
            Event::fire(self::EVENT_IS_CMS, array(&$this));
81
        }
82
    }
83
84
    /**
85
     * Callback for adding SamsonCMS related modules prefix to routes.
86
     *
87
     * @param $module
88
     * @param $prefix
89
     */
90
    public function updateCMSPrefix($module, &$prefix)
91
    {
92
        if (($module->id != $this->id) && $this->ifModuleRelated($module)) {
93
            $prefix = '/' . $this->id . $prefix;
94
        }
95
    }
96
97
    public function buildUrl(&$urlObj, &$httpHost, &$urlParams)
98
    {
99
        if ($this->isCMS) {
100
            array_unshift($urlParams, $this->id);
101
        }
102
    }
103
104
    public function parseUrl(&$urlObj, &$urlArgs)
105
    {
106
        if ($this->isCMS) {
107
            array_shift($urlArgs);
108
        }
109
    }
110
111
    /**
112
     * Check if passed module is related to SamsonCMS.
113
     * Also method stores data to flag variable.
114
     *
115
     * @param $module
116
     *
117
     * @return bool True if module related to SamsonCMS
118
     */
119
    public function ifModuleRelated($module)
120
    {
121
        // Analyze if module class or one of its parents has samsoncms\ namespace pattern
122
        return sizeof(preg_grep('/samsoncms\\\\/i', array_merge(array(get_class($module)), class_parents($module))));
123
    }
124
125
    public function __base()
126
    {
127
        $templateModule = $this->system->module('template');
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\cms\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...
128
129
        // Switch system to SamsonCMS template module
130
        $this->system->active($templateModule);
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\cms\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...
131
132
        // Call template handler
133
        $templateModule->__handler();
134
    }
135
136
    public function oldMainRenderer(&$html)
137
    {
138
        // Render application main page block
139
        foreach (\samsoncms\Application::loaded() as $app) {
0 ignored issues
show
Deprecated Code introduced by
The method samsoncms\Application::loaded() has been deprecated.

This method has been deprecated.

Loading history...
140
            // Show only visible apps
141
            if ($app->hide == false && $app->findView('sub_menu')) {
142
                $html .= $app->main();
143
            }
144
        }
145
    }
146
147
    /**
148
     * @deprecated All application should draw menu block via events
149
     */
150
    public function oldMenuRenderer(&$html, &$subMenu)
151
    {
152
        // Iterate loaded samson\cms\application
153
        foreach (\samsoncms\Application::loaded() as $app) {
0 ignored issues
show
Deprecated Code introduced by
The method samsoncms\Application::loaded() has been deprecated.

This method has been deprecated.

Loading history...
154
            // Show only visible apps
155
            if ($app->hide == false) {
156
                // Render application menu item
157
                $html .= m('template')
158
                    ->view('menu/item')
159
                    ->active(url()->module == $app->id() ? 'active' : '')
160
                    ->app($app)
161
                    ->icon($app->icon)
162
                    ->name(isset($app->name{0}) ? $app->name : '')
163
                    ->output();
164
            }
165
        }
166
        $subMenu = '';
167
        // Find current SamsonCMS application
168
        if (\samsoncms\Application::find(url()->module, $app/*@var $app App*/)) {
169
            // If module has sub_menu view - render it
170
            if ($app->findView('sub_menu')) {
171
                $subMenu .= $app->view('sub_menu')->output();
172
            }
173
        }
174
    }
175
176
    /**
177
     * @deprecated
178
     * @return string Page title
179
     */
180
    public function oldGetTitle()
181
    {
182
        $local   = m('local');
183
        $current = m();
184
185
        return isset($current['title']) ? $current['title'] :
186
            (isset($local['title']) ? $local['title'] : '');
187
    }
188
}
189