CoreFiles by mySites.guru
ContainerAwareToolbarFactory.php117 lines, 2,729 bytes, from Joomla_4.0.0-alpha2-Alpha-Full_Package.zip
Raw History Compare with 3.10.0-alpha9
md5
b7a8e5bfd5fedb8a04a7a5f9833f0f8e
sha1
77b50fb7021fb0a55a9559c8760810fb7f35f2aa
sha256
6fcd23e5523cc599bcea1883424ded5f22c38ad6bd6ad5d9337a6a83933cbda1
<?php
/**
 * Joomla! Content Management System
 *
 * @copyright  Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Toolbar;

defined('_JEXEC') or die;

use Joomla\DI\ContainerAwareInterface;
use Joomla\DI\ContainerAwareTrait;

/**
 * Default factory for creating toolbar objects
 *
 * @since  __DEPLOY_VERSION__
 */
class ContainerAwareToolbarFactory implements ToolbarFactoryInterface, ContainerAwareInterface
{
	use ContainerAwareTrait;

	/**
	 * Creates a new toolbar button.
	 *
	 * @param   Toolbar  $toolbar  The Toolbar instance to attach to the button
	 * @param   string   $type     Button Type
	 *
	 * @return  ToolbarButton
	 *
	 * @since   3.8.0
	 * @throws  \InvalidArgumentException
	 */
	public function createButton(Toolbar $toolbar, string $type): ToolbarButton
	{
		$buttonClass = $this->loadButtonClass($type);

		if (!$buttonClass)
		{
			$dirs = $toolbar->getButtonPath();

			$file = \JFilterInput::getInstance()->clean(str_replace('_', DIRECTORY_SEPARATOR, strtolower($type)) . '.php', 'path');

			jimport('joomla.filesystem.path');

			if ($buttonFile = \JPath::find($dirs, $file))
			{
				include_once $buttonFile;
			}
			else
			{
				\JLog::add(\JText::sprintf('JLIB_HTML_BUTTON_NO_LOAD', $buttonClass, $buttonFile), \JLog::WARNING, 'jerror');

				throw new \InvalidArgumentException(\JText::sprintf('JLIB_HTML_BUTTON_NO_LOAD', $buttonClass, $buttonFile));
			}
		}

		if (!class_exists($buttonClass))
		{
			throw new \InvalidArgumentException(sprintf('Class `%1$s` does not exist, could not create a toolbar button.'));
		}

		// Check for a possible service from the container otherwise manually instantiate the class
		if ($this->getContainer()->has($buttonClass))
		{
			return $this->getContainer()->get($buttonClass);
		}

		return new $buttonClass($toolbar);
	}

	/**
	 * Creates a new Toolbar object.
	 *
	 * @param   string  $name  The toolbar name.
	 *
	 * @return  Toolbar
	 *
	 * @since   __DEPLOY_VERSION__
	 */
	public function createToolbar(string $name = 'toolbar'): Toolbar
	{
		return new Toolbar($name, $this);
	}

	/**
	 * Load the button class including the deprecated ones.
	 *
	 * @param   string  $type  Button Type
	 *
	 * @return  string|null
	 *
	 * @since   __DEPLOY_VERSION__
	 */
	private function loadButtonClass(string $type)
	{
		$buttonClasses = [
			'Joomla\\CMS\\Toolbar\\Button\\' . ucfirst($type) . 'Button',
			// @deprecated 5.0
			'JToolbarButton' . ucfirst($type),
		];

		foreach ($buttonClasses as $buttonClass)
		{
			if (!class_exists($buttonClass))
			{
				continue;
			}

			return $buttonClass;
		}

		return null;
	}
}

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