Skip to content

Commit 6995205

Browse files
committed
if no component entrypoint use a default
1 parent facaa52 commit 6995205

File tree

1 file changed

+82
-6
lines changed

1 file changed

+82
-6
lines changed

core/libraries/Hubzero/Component/Loader.php

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Hubzero\Component;
99

10+
use Hubzero\Component\Router\DefaultRouter;
1011
use Hubzero\Component\Router\Legacy;
1112
use Hubzero\Container\Container;
1213
use Hubzero\Config\Registry;
@@ -210,6 +211,7 @@ public function render($option, $params = array())
210211
{
211212
$found = true;
212213
$path = $namespace;
214+
$type = 'bootstrap';
213215

214216
// Infer the appropriate language path and load from there
215217
$file = with(new \ReflectionClass($namespace))->getFileName();
@@ -222,6 +224,7 @@ public function render($option, $params = array())
222224
else if (is_file($path))
223225
{
224226
$found = true;
227+
$type = 'path';
225228

226229
// Load local language files
227230
$lang->load($option, PATH_COMPONENT, null, false, true);
@@ -230,6 +233,16 @@ public function render($option, $params = array())
230233
{
231234
$found = true;
232235
$path = $react_path;
236+
$ype = 'react';
237+
238+
// Load local language files
239+
$lang->load($option, PATH_COMPONENT, null, false, true);
240+
}
241+
else if (is_dir(PATH_COMPONENT))
242+
{
243+
$found = true;
244+
$path = $option;
245+
$type = 'default';
233246

234247
// Load local language files
235248
$lang->load($option, PATH_COMPONENT, null, false, true);
@@ -249,7 +262,7 @@ public function render($option, $params = array())
249262
$contents = null;
250263

251264
// Execute the component.
252-
$contents = $this->execute($path);
265+
$contents = $this->execute($path, $type);
253266

254267
// Revert the scope
255268
$this->app->forget('scope');
@@ -264,19 +277,23 @@ public function render($option, $params = array())
264277
* @param string $path The component path.
265278
* @return string The component output
266279
*/
267-
protected function execute($path)
280+
protected function execute($path, $type = 'path')
268281
{
269282
ob_start();
270283

271-
if (is_file($path))
284+
if ($type == 'path')
272285
{
273286
$this->executePath($path);
274287
}
275-
else if (is_dir($path))
288+
else if ($type == 'react')
276289
{
277290
$this->executeReactApp($path);
278291
}
279-
else
292+
else if ($type == 'default')
293+
{
294+
$this->executeDefault($path);
295+
}
296+
else if ($type == 'bootstrap')
280297
{
281298
$this->executeBootstrap($path);
282299
}
@@ -298,6 +315,55 @@ protected function executePath($path)
298315
require_once $path;
299316
}
300317

318+
/**
319+
* Start component without explicit entrypoint file
320+
*
321+
* @param string $namespace The namespace of the component to start
322+
* @return void
323+
*/
324+
private function executeDefault($option = '')
325+
{
326+
$option = (string) $option;
327+
328+
if (substr($option, 0, 4) == 'com_')
329+
{
330+
$component = substr($option, 4);
331+
}
332+
else
333+
{
334+
$component = $option;
335+
}
336+
337+
if ( preg_match("/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/", $component) !== 1 )
338+
{
339+
throw new \InvalidArgumentException(sprintf('Invalid component name [%s] requested', htmlspecialchars($component, ENT_HTML5)), 404);
340+
}
341+
342+
$controller = \Request::getCmd('controller', $component);
343+
344+
if ( preg_match("/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/", $controller) !== 1 )
345+
{
346+
throw new \InvalidArgumentException(sprintf('Invalid controller name [%s] requested', htmlspecialchars($controller, ENT_HTML5)), 404);
347+
}
348+
349+
$path = PATH_COMPONENT;
350+
351+
$config = array('base_path' => PATH_COMPONENT);
352+
$namespace = 'Components\\' . ucfirst($component) . '\Site\Controllers';
353+
$class = ucfirst($controller);
354+
355+
if (file_exists($path . "/controllers/{$controller}.php"))
356+
{
357+
require_once($path . "/controllers/{$controller}.php");
358+
}
359+
else
360+
{
361+
eval("namespace {$namespace}; class {$class} extends \\Hubzero\\Component\\DefaultSiteController\n{\n}\n");
362+
}
363+
364+
(new ($namespace.'\\'.$class)($config))->execute();
365+
}
366+
301367
/**
302368
* Execute the component from a new bootstrapped component
303369
*
@@ -460,7 +526,17 @@ public function router($option, $client = null, $version = null)
460526

461527
if (!isset(self::$routers[$key]))
462528
{
463-
self::$routers[$key] = new Legacy($compname);
529+
$path = $this->path($option);
530+
$name = substr($option, 4);
531+
532+
if (!is_file("{$path}/{$name}.php") && !is_file("{$path}/controllers/{$name}.php"))
533+
{
534+
self::$routers[$key] = new DefaultRouter($compname);
535+
}
536+
else
537+
{
538+
self::$routers[$key] = new Legacy($compname);
539+
}
464540
}
465541
}
466542

0 commit comments

Comments
 (0)