CoreFiles by mySites.guru
ExtensionManagerTrait.php179 lines, 4,338 bytes, from Joomla_4.0.0-alpha5-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha3
md5
5979677eca1fc1ed6c8f54398f9bb8ec
sha1
efacbf3579e4a8b44910a28e6b9f9b4af2390702
sha256
b78c4b29da959cf0fb6cc63fba4d762861bd6d21389b3954900f508706f34b8c
<?php
/**
 * Joomla! Content Management System
 *
 * @copyright  Copyright (C) 2005 - 2018 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
{
	/**
	 * The loaded extensions.
	 *
	 * @var array
	 */
	private $extensions = [ModuleInterface::class => [], ComponentInterface::class => []];

	/**
	 * 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($this->extensions[$type][$extensionName]))
		{
			return $this->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
		$this->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();
}

The same content is in 2 releases, Joomla 4.0.0-alpha5 to 4.0.0-alpha6. See them all, or every version of this file.