- md5
- e8b816de24b381fb06d3bd92d54ffc58
- sha1
- 1e19fc1abfde3a79381b685db0ba6a19ab930d02
- sha256
- 3a5c8c114ecc590731431d35715f23c238f0287c2c5ae938c0c8ff63bedd6c20
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\CMS\Extension;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Dispatcher\ModuleDispatcherFactory;
use Joomla\CMS\Event\AbstractEvent;
use Joomla\DI\Container;
use Joomla\DI\Exception\ContainerNotFoundException;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
/**
* Trait for classes which can load extensions
*
* @since 4.0.0
*/
trait ExtensionManagerTrait
{
/**
* Boots the component with the given name.
*
* @param string $component The component to boot.
*
* @return ComponentInterface
*
* @since 4.0.0
*/
public function bootComponent($component): ComponentInterface
{
// Normalize the component name
$component = strtolower(str_replace('com_', '', $component));
// Path to to look for services
$path = JPATH_ADMINISTRATOR . '/components/com_' . $component;
return $this->loadExtension(ComponentInterface::class, $component, $path);
}
/**
* Boots the module with the given name.
*
* @param string $module The module to boot
* @param string $applicationName The application name
*
* @return ModuleInterface
*
* @since 4.0.0
*/
public function bootModule($module, $applicationName): ModuleInterface
{
// Normalize the module name
$module = strtolower(str_replace('mod_', '', $module));
// Path to to look for services
$path = JPATH_SITE . '/modules/mod_' . $module;
if ($applicationName === 'administrator')
{
$path = JPATH_ADMINISTRATOR . '/modules/mod_' . $module;
}
return $this->loadExtension(ModuleInterface::class, $module, $path);
}
/**
* Loads the extension.
*
* @param string $type The extension type
* @param string $extensionName The extension name
* @param string $extensionPath The path of the extension
*
* @return ComponentInterface|ModuleInterface
*
* @since 4.0.0
*/
private function loadExtension($type, $extensionName, $extensionPath)
{
// Check if the extension is already loaded
if (!empty(ExtensionHelper::$extensions[$type][$extensionName]))
{
return ExtensionHelper::$extensions[$type][$extensionName];
}
// The container to get the services from
$container = $this->getContainer()->createChild();
$container->get(DispatcherInterface::class)->dispatch(
'onBeforeExtensionBoot',
AbstractEvent::create(
'onBeforeExtensionBoot',
[
'subject' => $this,
'type' => $type,
'extensionName' => $extensionName,
'container' => $container
]
)
);
// The path of the loader file
$path = $extensionPath . '/services/provider.php';
if (file_exists($path))
{
// Load the file
$provider = require_once $path;
// Check if the extension supports the service provider interface
if ($provider instanceof ServiceProviderInterface)
{
$provider->register($container);
}
}
// Fallback to legacy
if (!$container->has($type))
{
switch ($type)
{
case ComponentInterface::class:
$container->set($type, new LegacyComponent('com_' . $extensionName));
break;
case ModuleInterface::class:
$container->set($type, new Module(new ModuleDispatcherFactory('')));
break;
}
}
$container->get(DispatcherInterface::class)->dispatch(
'onAfterExtensionBoot',
AbstractEvent::create(
'onAfterExtensionBoot',
[
'subject' => $this,
'type' => $type,
'extensionName' => $extensionName,
'container' => $container
]
)
);
$extension = $container->get($type);
if ($extension instanceof BootableExtensionInterface)
{
$extension->boot($container);
}
// Cache the extension
ExtensionHelper::$extensions[$type][$extensionName] = $extension;
return $extension;
}
/**
* Get the DI container.
*
* @return Container
*
* @since 4.0.0
* @throws ContainerNotFoundException May be thrown if the container has not been set.
*/
abstract protected function getContainer();
}
No other release has this exact content. See every version of this file.