Completed
Push — master ( c34ef8...6dedf5 )
by Nikita
05:04
created

Application::updateCMSPrefix()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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