Issues (1474)

framework/Util/TPluginModule.php (7 issues)

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

70
		parent::init(/** @scrutinizer ignore-type */ $config);
Loading history...
71
	}
72
73
	/**
74
	 * Called onBeginRequest
75
	 * @param mixed $sender
76
	 * @param mixed $param
77
	 */
78
	public function attachPageServiceBehavior($sender, $param)
0 ignored issues
show
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

78
	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...
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

78
	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...
79
	{
80
		$service = $this->getService();
81
		if ($service instanceof \Prado\Web\Services\TPageService) {
82
			$service->attachEventHandler('onAdditionalPagePaths', [$this, 'additionalPagePaths']);
83
		}
84
	}
85
86
	/**
87
	 * additionalPagePaths returns possible alternative paths for the $pagePath
88
	 * @param \Prado\Web\Services\TPageService $service
89
	 * @param string $pagePath
90
	 */
91
	public function additionalPagePaths($service, $pagePath)
92
	{
93
		return $this->getPluginPagesPath() . DIRECTORY_SEPARATOR . strtr($pagePath, '.', DIRECTORY_SEPARATOR);
94
	}
95
96
	/**
97
	 * @return null|string the path of the error file for the plugin module
98
	 */
99
	public function getErrorFile()
100
	{
101
		$errorFile = $this->getPluginPath() . DIRECTORY_SEPARATOR . 'errorMessages.txt';
102
		if (is_file($errorFile)) {
103
			return $errorFile;
104
		}
105
		return null;
106
	}
107
108
	/**
109
	 * @return string path of the plugin
110
	 */
111
	public function getPluginPath()
112
	{
113
		if ($this->_pluginPath === null) {
114
			$reflect = new ReflectionClass($this::class);
115
			$this->_pluginPath = dirname($reflect->getFileName());
116
		}
117
		return $this->_pluginPath;
118
	}
119
120
	/**
121
	 * @param string $path
122
	 */
123
	public function setPluginPath($path)
124
	{
125
		$this->_pluginPath = $path;
126
	}
127
128
	/**
129
	 * @return string the path of the Pages director for the plugin, if available
130
	 */
131
	public function getPluginPagesPath()
132
	{
133
		if ($this->_pagesPath === null) {
134
			$path = $this->getPluginPath();
135
			$basePath = $path . DIRECTORY_SEPARATOR . $this->_relativePagesPath;
136
			if (($this->_pagesPath = realpath($basePath)) === false || !is_dir($this->_pagesPath)) {
137
				$basePath = $path . DIRECTORY_SEPARATOR . strtolower($this->_relativePagesPath);
138
				if (($this->_pagesPath = realpath($basePath)) === false || !is_dir($this->_pagesPath)) {
139
					$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...
140
				}
141
			}
142
		}
143
		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...
144
	}
145
146
	/**
147
	 * @param string $path
148
	 */
149
	public function setPluginPagesPath($path)
150
	{
151
		$this->_pagesPath = TPropertyValue::ensureString($path);
152
		$this->_relativePagesPath = null;
153
	}
154
155
	/**
156
	 * @return string relative path from PluginPath
157
	 */
158
	public function getRelativePagesPath()
159
	{
160
		if ($this->_relativePagesPath === null) {
161
			if (stripos($this->_pagesPath, $this->getPluginPath()) === 0) {
162
				$this->_relativePagesPath = substr($this->_pagesPath, strlen($this->getPluginPath()) + 1);
163
			}
164
		}
165
		return $this->_relativePagesPath;
166
	}
167
168
	/**
169
	 * @param string $relativePagesPath relative path of PluginPagesPath from PluginPath
170
	 */
171
	public function setRelativePagesPath($relativePagesPath)
172
	{
173
		$this->_relativePagesPath = TPropertyValue::ensureString($relativePagesPath);
174
		$this->_pagesPath = null;
175
	}
176
}
177