CoreFiles by mySites.guru
ExtensionManagerTrait.php144 lines, 3,414 bytes, from Joomla_4.0.0-alpha3-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha2
md5
ac89a5ea02f3bd0919ee5ba338818339
sha1
9230240bb61752b09913d6e0f7406c34537433cb
sha256
27b1a1b7a8e51af241cf6772ced4bf2922ce88b30b397d1e42b236a03a69d450
<?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\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 = [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);
	}

	/**
	 * 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
	 *
	 * @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) && $type == ComponentInterface::class)
		{
			$container->set($type, new LegacyComponent('com_' . $extensionName));
		}

		$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();
}

No other release has this exact content. See every version of this file.