Passed
Push — master ( 06f7cb...c3c92a )
by Fabio
05:08
created

TPluginModule::getPluginPagesPath()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 13
rs 9.2222
1
<?php
2
3
/**
4
 * TPluginModule class file.
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @package Prado
10
 */
11
	
12
namespace Prado\Util;
13
14
use Prado\Exceptions\TException;
15
use Prado\TPropertyValue;
16
use Prado\Util\Behaviors\TPageServiceExtraPathsBehavior;
0 ignored issues
show
Bug introduced by
The type Prado\Util\Behaviors\TPa...rviceExtraPathsBehavior was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use ReflectionClass;
18
19
/**
20
 * TPluginModule class.
21
 *
22
 * TPluginModule is for extending PRADO through Composer packages.  This installs
23
 * its own Pages, where available, and its own error message file for the module.
24
 *
25
 * Plugin pages should implement their *.page with the following page code:
26
 * <code>
27
 *	<com:TContent ID=<%$ PluginContentId %>>
28
 *		...  your page content ...
29
 * 	</com:TContent>
30
 * </code>
31
 * The 'PluginContentId' application parameter is what all plugin pages should implement as
32
 * the page TContent ID.  This way all plugin pages can be changed to the page MasterClass
33
 * layout TContentPlaceHolder for the application and layout. For example in the application
34
 * configuration file:
35
 * <code>
36
 *	<parameters>
37
 *	 <parameter id="PluginContentId" value="Main" />
38
 *  </parameters>
39
 * </code>
40
 * @author Brad Anderson <[email protected]>
41
 * @package Prado
42
 * @since 4.2.0
43
 */
44
45
class TPluginModule extends \Prado\TModule implements IPluginModule
46
{
47
	/** Module pages directory for finding pages of a module	 */
48
	public const PAGES_DIRECTORY = 'Pages';
49
	
50
	/** @var string path of the plugin */
51
	private $_pluginPath;
52
	
53
	/** @var string path of the pages folder for the plugin*/
54
	private $_pagesPath;
55
	
56
	/** @var string relative pages path to $_pluginPath */
57
	private $_relativePagesPath = self::PAGES_DIRECTORY;
58
	
59
	/**
60
	 * initializes the plugin module, looks for a Pages directory and adds a new {@link TBehavior}
61
	 * to help TPageService find any plugin module pages
62
	 * @param array $config this is the manifest for the plugin module
63
	 */
64
	public function init($config)
65
	{
66
		if ($this->getPluginPagesPath() !== null) {
0 ignored issues
show
introduced by
The condition $this->getPluginPagesPath() !== null is always true.
Loading history...
67
			$this->getApplication()->attachEventHandler('onBeginRequest', [$this, 'attachPageServiceBehavior']);
68
		}
69
		
70
		if ($errorFile = $this->getErrorFile()) {
71
			TException::addMessageFile($errorFile);
72
		}
73
		parent::init($config);
0 ignored issues
show
Bug introduced by
$config of type array is incompatible with the type Prado\Xml\TXmlElement expected by parameter $config of Prado\TModule::init(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
		parent::init(/** @scrutinizer ignore-type */ $config);
Loading history...
74
	}
75
	
76
	/**
77
	 * Called onBeginRequest
78
	 * @param array $config this is the manifest for the plugin module
79
	 * @param mixed $sender
80
	 * @param mixed $param
81
	 */
82
	public function attachPageServiceBehavior($sender, $param)
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
	public function attachPageServiceBehavior($sender, /** @scrutinizer ignore-unused */ $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sender is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
	public function attachPageServiceBehavior(/** @scrutinizer ignore-unused */ $sender, $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
	{
84
		$service = $this->getService();
85
		if ($service->isa('Prado\\Web\\Services\\TPageService')) {
0 ignored issues
show
Bug introduced by
The method isa() does not exist on Prado\IService. Since it exists in all sub-types, consider adding an abstract or default implementation to Prado\IService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
		if ($service->/** @scrutinizer ignore-call */ isa('Prado\\Web\\Services\\TPageService')) {
Loading history...
86
			$service->onAdditionalPagePaths[] = [$this, 'additionalPagePaths'];
0 ignored issues
show
Bug introduced by
Accessing onAdditionalPagePaths on the interface Prado\IService suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
87
		}
88
	}
89
	
90
	/**
91
	 * additionalPagePaths returns possible alternative paths for the $pagePath
92
	 * @param Prado\Wob\Services\TPageService $service
0 ignored issues
show
Bug introduced by
The type Prado\Util\Prado\Wob\Services\TPageService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
	 * @param string $pagePath
94
	 */
95
	public function additionalPagePaths($service, $pagePath)
96
	{
97
		return $this->getPluginPagesPath() . DIRECTORY_SEPARATOR . strtr($pagePath, '.', DIRECTORY_SEPARATOR);
98
	}
99
	
100
	
101
	/**
102
	 * @return null|string the path of the error file for the plugin module
103
	 */
104
	public function getErrorFile()
105
	{
106
		$errorFile = $this->getPluginPath() . DIRECTORY_SEPARATOR . 'errorMessages.txt';
107
		if (is_file($errorFile)) {
108
			return $errorFile;
109
		}
110
		return null;
111
	}
112
	
113
	/**
114
	 * @return string path of the plugin
115
	 */
116
	public function getPluginPath()
117
	{
118
		if ($this->_pluginPath === null) {
119
			$reflect = new ReflectionClass(get_class($this));
120
			$this->_pluginPath = dirname($reflect->getFileName());
121
		}
122
		return $this->_pluginPath;
123
	}
124
	
125
	/**
126
	 * @param mixed $path
127
	 * @return string path of the plugin
128
	 */
129
	public function setPluginPath($path)
130
	{
131
		$this->_pluginPath = $path;
132
	}
133
	
134
	/**
135
	 * @return string the path of the Pages director for the plugin, if available
136
	 */
137
	public function getPluginPagesPath()
138
	{
139
		if ($this->_pagesPath === null) {
140
			$path = $this->getPluginPath();
141
			$basePath = $path . DIRECTORY_SEPARATOR . $this->_relativePagesPath;
142
			if (($this->_pagesPath = realpath($basePath)) === false || !is_dir($this->_pagesPath)) {
143
				$basePath = $path . DIRECTORY_SEPARATOR . strtolower($this->_relativePagesPath);
144
				if (($this->_pagesPath = realpath($basePath)) === false || !is_dir($this->_pagesPath)) {
145
					$this->_pagesPath = false;
0 ignored issues
show
Documentation Bug introduced by
The property $_pagesPath was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
146
				}
147
			}
148
		}
149
		return $this->_pagesPath;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_pagesPath could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
150
	}
151
	
152
	/**
153
	 * @param string $path
154
	 */
155
	public function setPluginPagesPath($path)
156
	{
157
		$this->_pagesPath = TPropertyValue::ensureString($path);
158
		$this->_relativePagesPath = null;
159
	}
160
	
161
	/**
162
	 * @return string relative path from PluginPath
163
	 */
164
	public function getRelativePagesPath()
165
	{
166
		if ($this->_relativePagesPath === null) {
167
			if (stripos($this->_pagesPath, $this->getPluginPath()) === 0) {
168
				$this->_relativePagesPath = substr($this->_pagesPath, strlen($this->getPluginPath()) + 1);
169
			}
170
		}
171
		return $this->_relativePagesPath;
172
	}
173
	
174
	/**
175
	 * @param string $relativePagesPath relative path of PluginPagesPath from PluginPath
176
	 */
177
	public function setRelativePagesPath($relativePagesPath)
178
	{
179
		$this->_relativePagesPath = TPropertyValue::ensureString($relativePagesPath);
180
		$this->_pagesPath = null;
181
	}
182
}
183