CoreFiles by mySites.guru
ApiApplication.php340 lines, 8,988 bytes, from Joomla_4.0.0-beta1-Beta-Full_Package.zip
Raw History Compare with 3.9.0-beta4
md5
e901b4b1ae491575c977a751dbbebe7a
sha1
5c730ad28f94bce589d877c16bd18e44758f3118
sha256
fc0c839f0ca33eb55f5b99bd76caf6bbbb924057337e34c17d04f691d8cf3489
<?php
/**
 * @package    Joomla.API
 *
 * @copyright  Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE
 */

namespace Joomla\CMS\Application;

\defined('JPATH_PLATFORM') or die;

use Joomla\Application\Web\WebClient;
use Joomla\CMS\Access\Exception\AuthenticationFailed;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\ApiRouter;
use Joomla\CMS\Router\Exception\RouteNotFoundException;
use Joomla\CMS\Uri\Uri;
use Joomla\DI\Container;
use Joomla\Input\Json as JInputJson;
use Joomla\Registry\Registry;
use Negotiation\Accept;
use Negotiation\Exception\InvalidArgument;
use Negotiation\Negotiator;

/**
 * Joomla! API Application class
 *
 * @since  4.0.0
 */
final class ApiApplication extends CMSApplication
{
	/**
	 * Maps extension types to their
	 *
	 * @var    array
	 * @since  4.0.0
	 */
	protected $formatMapper = array();

	/**
	 * The authentication plugin type
	 *
	 * @var    string
	 * @since  4.0.0
	 */
	protected $authenticationPluginType = 'api-authentication';

	/**
	 * Class constructor.
	 *
	 * @param   JInputJson  $input      An optional argument to provide dependency injection for the application's input
	 *                                  object.  If the argument is a JInput object that object will become the
	 *                                  application's input object, otherwise a default input object is created.
	 * @param   Registry    $config     An optional argument to provide dependency injection for the application's config
	 *                                  object.  If the argument is a Registry object that object will become the
	 *                                  application's config object, otherwise a default config object is created.
	 * @param   WebClient   $client     An optional argument to provide dependency injection for the application's client
	 *                                  object.  If the argument is a WebClient object that object will become the
	 *                                  application's client object, otherwise a default client object is created.
	 * @param   Container   $container  Dependency injection container.
	 *
	 * @since   4.0.0
	 */
	public function __construct(JInputJson $input = null, Registry $config = null, WebClient $client = null, Container $container = null)
	{
		// Register the application name
		$this->name = 'api';

		// Register the client ID
		$this->clientId = 3;

		// Execute the parent constructor
		parent::__construct($input, $config, $client, $container);

		$this->addFormatMap('application/json', 'json');
		$this->addFormatMap('application/vnd.api+json', 'jsonapi');

		// Set the root in the URI based on the application name
		Uri::root(null, str_ireplace('/' . $this->getName(), '', Uri::base(true)));
	}


	/**
	 * Method to run the application routines.
	 *
	 * Most likely you will want to instantiate a controller and execute it, or perform some sort of task directly.
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	protected function doExecute()
	{
		// Initialise the application
		$this->initialiseApp();

		// Mark afterInitialise in the profiler.
		JDEBUG ? $this->profiler->mark('afterInitialise') : null;

		// Route the application
		$this->route();

		// Mark afterApiRoute in the profiler.
		JDEBUG ? $this->profiler->mark('afterApiRoute') : null;

		// Dispatch the application
		$this->dispatch();

		// Mark afterDispatch in the profiler.
		JDEBUG ? $this->profiler->mark('afterDispatch') : null;
	}

	/**
	 * Adds a mapping from a content type to the format stored. Note the format type cannot be overwritten.
	 *
	 * @param   string  $contentHeader  The content header
	 * @param   string  $format         The content type format
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	public function addFormatMap($contentHeader, $format)
	{
		if (!\array_key_exists($contentHeader, $this->formatMapper))
		{
			$this->formatMapper[$contentHeader] = $format;
		}
	}

	/**
	 * Rendering is the process of pushing the document buffers into the template
	 * placeholders, retrieving data from the document and pushing it into
	 * the application response buffer.
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 *
	 * @note    Rendering should be overridden to get rid of the theme files.
	 */
	protected function render()
	{
		// Render the document
		$this->setBody($this->document->render($this->allowCache()));
	}

	/**
	 * Method to send the application response to the client.  All headers will be sent prior to the main application output data.
	 *
	 * @param   array  $options  An optional argument to enable CORS. (Temporary)
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	protected function respond($options = array())
	{
		// Set the Joomla! API signature
		$this->setHeader('X-Powered-By', 'JoomlaAPI/1.0', true);

		if (\array_key_exists('cors', $options))
		{
			// Enable CORS (Cross-origin resource sharing)
			$this->setHeader('Access-Control-Allow-Origin', '*', true);
			$this->setHeader('Access-Control-Allow-Headers', 'Authorization');
		}

		// Parent function can be overridden later on for debugging.
		parent::respond();

	}

	/**
	 * Gets the name of the current template.
	 *
	 * @param   boolean  $params  True to return the template parameters
	 *
	 * @return  string
	 *
	 * @since   4.0.0
	 */
	public function getTemplate($params = false)
	{
		// The API application should not need to use a template
		return 'system';
	}

	/**
	 * Route the application.
	 *
	 * Routing is the process of examining the request environment to determine which
	 * component should receive the request. The component optional parameters
	 * are then set in the request object to be processed when the application is being
	 * dispatched.
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	protected function route()
	{
		$router = $this->getApiRouter();

		// Trigger the onBeforeApiRoute event.
		PluginHelper::importPlugin('webservices');
		$this->triggerEvent('onBeforeApiRoute', array(&$router, $this));
		$caught404 = false;

		try
		{
			$route = $router->parseApiRoute($this->input->getMethod());
		}
		catch (RouteNotFoundException $e)
		{
			$caught404 = true;
		}

		/**
		 * Now we have an API perform content negotiation to ensure we have a valid header. Assume if the route doesn't
		 * tell us otherwise it uses the plain JSON API
		 */
		$priorities = array('application/vnd.api+json');

		if (!$caught404 && \array_key_exists('format', $route['vars']))
		{
			$priorities = $route['vars']['format'];
		}

		$negotiator = new Negotiator;

		try
		{
			$mediaType = $negotiator->getBest($this->input->server->getString('HTTP_ACCEPT'), $priorities);
		}
		catch (InvalidArgument $e)
		{
			$mediaType = null;
		}

		// If we can't find a match bail with a 406 - Not Acceptable
		if ($mediaType === null)
		{
			throw new Exception\NotAcceptable('Could not match accept header', 406);
		}

		/** @var $mediaType Accept */
		$format = $mediaType->getValue();

		if (\array_key_exists($mediaType->getValue(), $this->formatMapper))
		{
			$format = $this->formatMapper[$mediaType->getValue()];
		}

		$this->input->set('format', $format);

		if ($caught404)
		{
			throw $e;
		}

		$this->input->set('option', $route['vars']['component']);
		$this->input->set('controller', $route['controller']);
		$this->input->set('task', $route['task']);

		foreach ($route['vars'] as $key => $value)
		{
			if ($key !== 'component')
			{
				if ($this->input->getMethod() === 'POST')
				{
					$this->input->post->set($key, $value);
				}
				else
				{
					$this->input->set($key, $value);
				}
			}
		}

		$this->triggerEvent('onAfterApiRoute', array($this));

		if (!isset($route['vars']['public']) || $route['vars']['public'] === false)
		{
			if (!$this->login(array('username' => ''), array('silent' => true, 'action' => 'core.login.api')))
			{
				throw new AuthenticationFailed;
			}
		}
	}

	/**
	 * Returns the application Router object.
	 *
	 * @return  ApiRouter
	 *
	 * @since   4.0.0
	 */
	public function getApiRouter()
	{
		return $this->getContainer()->get('ApiRouter');
	}

	/**
	 * Dispatch the application
	 *
	 * @param   string  $component  The component which is being rendered.
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	public function dispatch($component = null)
	{
		// Get the component if not set.
		if (!$component)
		{
			$component = $this->input->get('option', null);
		}

		// Load the document to the API
		$this->loadDocument();

		// Set up the params
		$document = Factory::getDocument();

		// Register the document object with Factory
		Factory::$document = $document;

		$contents = ComponentHelper::renderComponent($component);
		$document->setBuffer($contents, 'component');

		// Trigger the onAfterDispatch event.
		PluginHelper::importPlugin('system');
		$this->triggerEvent('onAfterDispatch');
	}
}

The same content is in 3 releases, Joomla 4.0.0-beta to 4.0.0-beta3. See them all, or every version of this file.