Skip to content

Commit 7b5390b

Browse files
author
Volodymyr Shandak
committed
Let override even before onAfterRoute event
1 parent 39d95a1 commit 7b5390b

File tree

2 files changed

+130
-109
lines changed

2 files changed

+130
-109
lines changed

mvcoverride.php

+129-108
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class PlgSystemMVCOverride extends JPlugin
4444
*/
4545
protected static $option;
4646

47+
/**
48+
* @var array
49+
* @since 1.6
50+
*/
51+
protected $pathwaysFromTemplate = [];
52+
4753
/**
4854
* Constructor
4955
*
@@ -58,92 +64,131 @@ public function __construct(&$subject, $config = array())
5864
JPlugin::loadLanguage('plg_system_mvcoverride');
5965

6066
parent::__construct($subject, $config);
61-
}
6267

63-
/**
64-
* onAfterRoute function.
65-
*
66-
* @return void
67-
* @since 1.4
68-
*/
69-
public function onAfterRoute()
70-
{
71-
JPluginHelper::importPlugin('redcore');
72-
73-
$app = JFactory::getApplication();
74-
75-
$includePath = $this->params->get('includePath', '{JPATH_BASE}/code,{JPATH_THEMES}/{template}/code');
76-
77-
$includePath = str_replace(
78-
array('{JPATH_BASE}', '{JPATH_THEMES}', '{template}'),
79-
array(JPATH_BASE, JPATH_THEMES, $app->getTemplate()),
80-
$includePath
81-
);
68+
$redcoreLoader = JPATH_LIBRARIES . '/redcore/bootstrap.php';
8269

83-
$includePath = explode(',', $includePath);
84-
85-
// Register additional include paths for code replacements from plugins
86-
$app->triggerEvent('onMVCOverrideIncludePaths', array(&$includePath));
87-
88-
MVCOverrideHelperCodepool::addCodePath($includePath);
70+
// Try to support redCORE RLoader if exist
71+
if (file_exists($redcoreLoader))
72+
{
73+
require_once $redcoreLoader;
74+
RBootstrap::bootstrap(false);
75+
}
8976

9077
MVCLoader::setupOverrideLoader(
9178
$this->params->get('changePrivate', 0),
9279
$this->params->get('extendPrefix', ''),
9380
$this->params->get('extendSuffix', 'Default')
9481
);
82+
MVCOverrideHelperCodepool::initialize();
83+
84+
$includedPathways = $this->params->get('includePath', '{JPATH_BASE}/code,{JPATH_THEMES}/{template}/code');
85+
86+
$includedPathways = str_replace(
87+
['{JPATH_BASE}', '{JPATH_THEMES}'],
88+
[JPATH_BASE, JPATH_THEMES],
89+
$includedPathways
90+
);
9591

96-
$this->setOverrideFiles();
97-
$option = $this->getOption();
98-
$hasOption = true;
92+
$includedPathways = explode(',', $includedPathways);
9993

100-
if ($option === false || !isset(self::$componentList[$option]))
94+
foreach ($includedPathways as $key => $includedPatch)
10195
{
102-
$hasOption = false;
96+
if (strpos($includedPatch, '{template}') !== false)
97+
{
98+
// We will attach it in onAfterRoute
99+
$this->pathwaysFromTemplate[] = $includedPatch;
100+
unset($includedPathways[$key]);
101+
}
103102
}
104103

105-
MVCOverrideHelperCodepool::initialize();
104+
$this->initialiseComponents($includedPathways);
105+
}
106+
107+
/**
108+
* @param array|string $includedPathways Included pathways
109+
*
110+
* @since 1.6.0
111+
* @return void
112+
*/
113+
protected function initialiseComponents($includedPathways)
114+
{
115+
if (empty($includedPathways))
116+
{
117+
return;
118+
}
106119

107120
// Add override paths for the current component files
108-
foreach (MVCOverrideHelperCodepool::addCodePath() as $codePool)
121+
foreach ((array) $includedPathways as $codePool)
109122
{
123+
if (!JFolder::exists($codePool))
124+
{
125+
continue;
126+
}
127+
128+
MVCOverrideHelperCodepool::addCodePath($codePool);
129+
130+
$components = JFolder::folders($codePool);
131+
132+
if (!empty($components))
133+
{
134+
foreach ($components as $key => $component)
135+
{
136+
if (strpos($component, 'com_') !== 0)
137+
{
138+
unset($components[$key]);
139+
continue;
140+
}
141+
142+
$this->addOverrideFiles($codePool, $component);
143+
}
144+
}
145+
110146
if (version_compare(JVERSION, '3.8', '>='))
111147
{
112-
if ($hasOption)
148+
if (!empty($components))
113149
{
114-
Joomla\CMS\MVC\View\HtmlView::addViewHelperPath($codePool . '/' . $option);
115-
Joomla\CMS\MVC\View\HtmlView::addViewTemplatePath($codePool . '/' . $option);
116-
Joomla\CMS\Table\Table::addIncludePath($codePool . '/' . $option . '/tables');
117-
Joomla\CMS\MVC\Model\FormModel::addComponentFormPath($codePool . '/' . $option . '/models/forms');
118-
Joomla\CMS\MVC\Model\FormModel::addComponentFieldPath($codePool . '/' . $option . '/models/fields');
119-
Joomla\CMS\MVC\Model\ListModel::addComponentFormPath($codePool . '/' . $option . '/models/forms');
120-
Joomla\CMS\MVC\Model\ListModel::addComponentFieldPath($codePool . '/' . $option . '/models/fields');
150+
foreach ($components as $option)
151+
{
152+
Joomla\CMS\MVC\View\HtmlView::addViewHelperPath($codePool . '/' . $option);
153+
Joomla\CMS\MVC\View\HtmlView::addViewTemplatePath($codePool . '/' . $option);
154+
Joomla\CMS\Table\Table::addIncludePath($codePool . '/' . $option . '/tables');
155+
Joomla\CMS\MVC\Model\FormModel::addComponentFormPath($codePool . '/' . $option . '/models/forms');
156+
Joomla\CMS\MVC\Model\FormModel::addComponentFieldPath($codePool . '/' . $option . '/models/fields');
157+
Joomla\CMS\MVC\Model\ListModel::addComponentFormPath($codePool . '/' . $option . '/models/forms');
158+
Joomla\CMS\MVC\Model\ListModel::addComponentFieldPath($codePool . '/' . $option . '/models/fields');
159+
}
121160
}
122161

123162
Joomla\CMS\Helper\ModuleHelper::addIncludePath($codePool . '/modules');
124163
}
125164
elseif (version_compare(JVERSION, '3.0', '>='))
126165
{
127-
if ($hasOption)
166+
if (!empty($components))
128167
{
129-
JViewLegacy::addViewHelperPath($codePool . '/' . $option);
130-
JViewLegacy::addViewTemplatePath($codePool . '/' . $option);
131-
JTable::addIncludePath($codePool . '/' . $option . '/tables');
132-
JModelForm::addComponentFormPath($codePool . '/' . $option . '/models/forms');
133-
JModelForm::addComponentFieldPath($codePool . '/' . $option . '/models/fields');
168+
foreach ($components as $option)
169+
{
170+
JViewLegacy::addViewHelperPath($codePool . '/' . $option);
171+
JViewLegacy::addViewTemplatePath($codePool . '/' . $option);
172+
JTable::addIncludePath($codePool . '/' . $option . '/tables');
173+
JModelForm::addComponentFormPath($codePool . '/' . $option . '/models/forms');
174+
JModelForm::addComponentFieldPath($codePool . '/' . $option . '/models/fields');
175+
}
134176
}
135177

136178
JModuleHelper::addIncludePath($codePool . '/modules');
137179
}
138180
else
139181
{
140-
if ($hasOption)
182+
if (!empty($components))
141183
{
142-
JView::addViewHelperPath($codePool . '/' . $option);
143-
JView::addViewTemplatePath($codePool . '/' . $option);
144-
JTable::addIncludePath($codePool . '/' . $option . '/tables');
145-
JModelForm::addComponentFormPath($codePool . '/' . $option . '/models/forms');
146-
JModelForm::addComponentFieldPath($codePool . '/' . $option . '/models/fields');
184+
foreach ($components as $option)
185+
{
186+
JView::addViewHelperPath($codePool . '/' . $option);
187+
JView::addViewTemplatePath($codePool . '/' . $option);
188+
JTable::addIncludePath($codePool . '/' . $option . '/tables');
189+
JModelForm::addComponentFormPath($codePool . '/' . $option . '/models/forms');
190+
JModelForm::addComponentFieldPath($codePool . '/' . $option . '/models/fields');
191+
}
147192
}
148193

149194
JModuleHelper::addIncludePath($codePool . '/modules');
@@ -152,26 +197,32 @@ public function onAfterRoute()
152197
}
153198

154199
/**
155-
* Set Override Files
200+
* onAfterRoute function.
156201
*
157-
* @return void
202+
* @return void
158203
* @since 1.4
204+
* @throws Exception
159205
*/
160-
public function setOverrideFiles()
206+
public function onAfterRoute()
161207
{
162-
$includePaths = MVCOverrideHelperCodepool::addCodePath(null);
208+
$app = JFactory::getApplication();
163209

164-
foreach ($includePaths as $includePath)
210+
if (!empty($this->pathwaysFromTemplate))
165211
{
166-
if ($components = JFolder::folders($includePath))
212+
foreach ($this->pathwaysFromTemplate as &$pathwayFromTemplate)
167213
{
168-
foreach ($components as $component)
169-
{
170-
self::$componentList[$component] = $component;
171-
$this->addOverrideFiles($includePath, $component);
172-
}
214+
$pathwayFromTemplate = str_replace(
215+
array('{template}'),
216+
array($app->getTemplate()),
217+
$pathwayFromTemplate
218+
);
173219
}
174220
}
221+
222+
// Register additional include paths for code replacements from plugins
223+
$app->triggerEvent('onMVCOverrideIncludePaths', array(&$this->pathwaysFromTemplate));
224+
225+
$this->initialiseComponents($this->pathwaysFromTemplate);
175226
}
176227

177228
/**
@@ -183,7 +234,7 @@ public function setOverrideFiles()
183234
* @return void
184235
* @since 1.4
185236
*/
186-
private function addOverrideFiles($includePath, $component)
237+
protected function addOverrideFiles($includePath, $component)
187238
{
188239
$types = array('controllers', 'models', 'helpers', 'views');
189240
$currentFormat = JFactory::getDocument()->getType();
@@ -202,7 +253,9 @@ private function addOverrideFiles($includePath, $component)
202253
switch ($type)
203254
{
204255
case 'helpers':
205-
if ($listFiles = JFolder::files($searchFolder, '.php', false, true))
256+
$listFiles = JFolder::files($searchFolder, '.php', false, true);
257+
258+
if (!empty($listFiles))
206259
{
207260
foreach ($listFiles as $file)
208261
{
@@ -215,12 +268,16 @@ private function addOverrideFiles($includePath, $component)
215268

216269
case 'views':
217270
// Reading view folders
218-
if ($views = JFolder::folders($searchFolder))
271+
$views = JFolder::folders($searchFolder);
272+
273+
if (!empty($views))
219274
{
220275
foreach ($views as $view)
221276
{
222277
// Get view formats files
223-
if ($listFiles = JFolder::files($searchFolder . '/' . $view, '.' . $currentFormat . '.php', false, true))
278+
$listFiles = JFolder::files($searchFolder . '/' . $view, '.' . $currentFormat . '.php', false, true);
279+
280+
if (!empty($listFiles))
224281
{
225282
foreach ($listFiles as $file)
226283
{
@@ -232,7 +289,9 @@ private function addOverrideFiles($includePath, $component)
232289
break;
233290

234291
default:
235-
if ($listFiles = JFolder::files($searchFolder, '.php', false, true))
292+
$listFiles = JFolder::files($searchFolder, '.php', false, true);
293+
294+
if (!empty($listFiles))
236295
{
237296
foreach ($listFiles as $file)
238297
{
@@ -255,7 +314,7 @@ private function addOverrideFiles($includePath, $component)
255314
* @return void
256315
* @since 1.4
257316
*/
258-
private function getOverrideFileInfo($includePath, $component, $filePath, $type = '', $indexName = '')
317+
protected function getOverrideFileInfo($includePath, $component, $filePath, $type = '', $indexName = '')
259318
{
260319
$filePath = JPath::clean($filePath);
261320
$sameFolderPrefix = $component . '/' . $type;
@@ -309,42 +368,4 @@ private function getOverrideFileInfo($includePath, $component, $filePath, $type
309368
$this->params->get('extendSuffix', 'Default')
310369
);
311370
}
312-
313-
/**
314-
* Get option
315-
*
316-
* @return boolean|mixed|string
317-
* @since 1.4
318-
*/
319-
private function getOption()
320-
{
321-
if (self::$option)
322-
{
323-
return self::$option;
324-
}
325-
326-
$app = JFactory::getApplication();
327-
self::$option = $app->input->getCmd('option', '');
328-
329-
if (empty(self::$option) && $app->isSite())
330-
{
331-
$menuDefault = JFactory::getApplication()->getMenu()->getDefault();
332-
333-
if (!$menuDefault)
334-
{
335-
return false;
336-
}
337-
338-
$componentID = $menuDefault->component_id;
339-
$db = JFactory::getDBO();
340-
$query = $db->getQuery(true)
341-
->select('element')
342-
->from($db->qn('#__extensions'))
343-
->where('extension_id = ' . $db->quote($componentID));
344-
$db->setQuery($query);
345-
self::$option = $db->loadResult();
346-
}
347-
348-
return self::$option;
349-
}
350371
}

mvcoverride.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<license>GNU General Public License version 2 or later, see LICENSE.</license>
88
<authorEmail>[email protected]</authorEmail>
99
<authorUrl>www.redcomponent.com</authorUrl>
10-
<version>1.5.0</version>
10+
<version>1.6.0</version>
1111
<description>PLG_SYSTEM_MVC_OVERRIDE_DESCRIPTION</description>
1212

1313
<files>

0 commit comments

Comments
 (0)