CoreFiles by mySites.guru
ContainerAwareToolbarFactory.php117 lines, 2,690 bytes, from Joomla_4.0.0-alpha3-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha2
md5
d7f83cc7055ad4d161f6a96efcdcef4e
sha1
03ff4e4504d3f1ef79b183be4fc1342fcf221c85
sha256
686e11f9315a871fe8353d213d9e5bbeb83eaa4f4a91fbf3351edf027f7b942e
<?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.txt
 */

namespace Joomla\CMS\Toolbar;

defined('_JEXEC') or die;

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

/**
 * Default factory for creating toolbar objects
 *
 * @since  4.0.0
 */
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   4.0.0
	 */
	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   4.0.0
	 */
	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.