Completed
Push — master ( 6dedf5...3a82d8 )
by Nikita
03:51
created

Application::isCMS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 ( ! (isset($module->composerParameters['composerName']) &&
42
                    isset($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...
43
                    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...
44
            ) {
45
                if ($id != 'core') {
46
                    unset($moduleList[$id]);
47
                }
48
            }
49
        }
50
51
        // Generate resources for new module
52
        $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...
53
        //[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...
54
55
        // Call parent initialization
56
        return parent::init($params);
57
    }
58
59
    public function isCMS()
60
    {
61
        return $this->isCMS;
62
    }
63
64
    public function activeModuleHandler($module)
65
    {
66
        // Define if routed module is related to SamsonCMS
67
        if($this->isCMS = $this->ifModuleRelated($module)){
68
            // TODO: This should be removed - Reparse url
69
            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...
70
71
            // Switch template to SamsonCMS
72
            $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...
73
74
            Event::fire(self::EVENT_IS_CMS, array(&$this));
75
        }
76
    }
77
78
    /**
79
     * Callback for adding SamsonCMS related modules prefix to routes.
80
     *
81
     * @param $module
82
     * @param $prefix
83
     */
84
    public function updateCMSPrefix($module, &$prefix)
85
    {
86
        if (($module->id != $this->id) && $this->ifModuleRelated($module)) {
87
            $prefix = '/' . $this->id . $prefix;
88
        }
89
    }
90
91
    public function buildUrl(&$urlObj, &$httpHost, &$urlParams)
92
    {
93
        if ($this->isCMS) {
94
            array_unshift($urlParams, $this->id);
95
        }
96
    }
97
98
    public function parseUrl(&$urlObj, &$urlArgs)
99
    {
100
        if ($this->isCMS) {
101
            array_shift($urlArgs);
102
        }
103
    }
104
105
    /**
106
     * Check if passed module is related to SamsonCMS.
107
     * Also method stores data to flag variable.
108
     *
109
     * @param $module
110
     *
111
     * @return bool True if module related to SamsonCMS
112
     */
113
    public function ifModuleRelated($module)
114
    {
115
        // Analyze if module class or one of its parents has samsoncms\ namespace pattern
116
        return sizeof(preg_grep('/samsoncms\\\\/i', array_merge(array(get_class($module)), class_parents($module))));
117
    }
118
119
    public function __base()
120
    {
121
        $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...
122
123
        // Switch system to SamsonCMS template module
124
        $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...
125
126
        // Call template handler
127
        $templateModule->__handler();
128
    }
129
130
    public function oldMainRenderer(&$html)
131
    {
132
        // Render application main page block
133
        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...
134
            // Show only visible apps
135
            if ($app->hide == false && $app->findView('sub_menu')) {
136
                $html .= $app->main();
137
            }
138
        }
139
    }
140
141
    /**
142
     * @deprecated All application should draw menu block via events
143
     */
144
    public function oldMenuRenderer(&$html, &$subMenu)
145
    {
146
        // Iterate loaded samson\cms\application
147
        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...
148
            // Show only visible apps
149
            if ($app->hide == false) {
150
                // Render application menu item
151
                $html .= m('template')
152
                    ->view('menu/item')
153
                    ->active(url()->module == $app->id() ? 'active' : '')
154
                    ->app($app)
155
                    ->icon($app->icon)
156
                    ->name(isset($app->name{0}) ? $app->name : '')
157
                    ->output();
158
            }
159
        }
160
        $subMenu = '';
161
        // Find current SamsonCMS application
162
        if (\samsoncms\Application::find(url()->module, $app/*@var $app App*/)) {
163
            // If module has sub_menu view - render it
164
            if ($app->findView('sub_menu')) {
165
                $subMenu .= $app->view('sub_menu')->output();
166
            }
167
        }
168
    }
169
170
    /**
171
     * @deprecated
172
     * @return string Page title
173
     */
174
    public function oldGetTitle()
175
    {
176
        $local   = m('local');
177
        $current = m();
178
179
        return isset($current['title']) ? $current['title'] :
180
            (isset($local['title']) ? $local['title'] : '');
181
    }
182
}
183