Skip to content

Commit

Permalink
Lib api (#20)
Browse files Browse the repository at this point in the history
* Added route function

* Added ApiRouter class

* Fixing phpcs errors

* Fixed coding standard issues, wrong namespace

* Added service provider for ApiRouter

* Changed the name of getRouter to getApiRouter

* Fixed some phpcs issues

* Codestyle

* Code style part 2

* Changed the delimiter of controller function

* Modified parseRoute method to parse the controller to be an array of class and method

* Fixed code standard issues

* Code style

* Added dispatch function, Edited parseRoute function

* Added request method to the route parsing function, removed some unnecessary code and called dispatch function in doExecute function

* Code style

* Added request variables to the input object

* Type safe comparison

* Removed the API component path constant

* Update ApiApplication.php
  • Loading branch information
muhakh authored and wilsonge committed Jul 17, 2017
1 parent 8e24a19 commit 2e1eec0
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 14 deletions.
74 changes: 73 additions & 1 deletion libraries/src/CMS/Application/ApiApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
defined('JPATH_PLATFORM') or die;

use Joomla\Application\Web\WebClient;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Router\ApiRouter;
use Joomla\DI\Container;
use Joomla\Registry\Registry;
Expand Down Expand Up @@ -71,6 +72,20 @@ 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;
}

/**
Expand Down Expand Up @@ -148,11 +163,32 @@ public function getTemplate($params = false)
protected function route()
{
$uri = \JUri::getInstance();
$router = static::getRouter();
$router = $this->getApiRouter();

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

$route = $router->parseRoute($uri::current(), $this->input->getMethod());

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

/**
Expand All @@ -166,4 +202,40 @@ public function getApiRouter()
{
return JFactory::getContainer()->get('ApiRouter');
}

/**
* Dispatch the application
*
* @param string $component The component which is being rendered.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function dispatch($component = null)
{
$app = \JFactory::getApplication();

// 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 = \JFactory::getDocument();

// Register the document object with \JFactory
\JFactory::$document = $document;

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

// Trigger the onAfterDispatch event.
PluginHelper::importPlugin('system');
$this->triggerEvent('onAfterDispatch');
}
}
13 changes: 8 additions & 5 deletions libraries/src/CMS/Component/ComponentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,14 @@ public static function renderComponent($option, $params = array())
{
$app = \JFactory::getApplication();

// Load template language files.
$template = $app->getTemplate(true)->template;
$lang = \JFactory::getLanguage();
$lang->load('tpl_' . $template, JPATH_BASE, null, false, true)
|| $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", null, false, true);
if (!$app->isClient('api'))
{
// Load template language files.
$template = $app->getTemplate(true)->template;
$lang = \JFactory::getLanguage();
$lang->load('tpl_' . $template, JPATH_BASE, null, false, true)
|| $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", null, false, true);
}

if (empty($option))
{
Expand Down
57 changes: 49 additions & 8 deletions libraries/src/CMS/Router/ApiRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
*/
class ApiRouter extends Router
{
/**
* Router instances container.
*
* @var array
* @since __DEPLOY_VERSION__
*/
protected static $instances = array();

/**
* Creates routes map for CRUD
*
Expand Down Expand Up @@ -87,4 +79,53 @@ public function createCRUDRoutes($baseName, $controller, $defaults = array())
);
$this->addRoutes($routes);
}

/**
* Parse the given route and return the name of a controller mapped to the given route.
*
* @param string $route The route string for which to find and execute a controller.
* @param string $method Request method to match. One of GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE or PATCH
*
* @return array An array containing the controller and the matched variables.
*
* @since __DEPLOY_VERSION__
* @throws \InvalidArgumentException
*/
public function parseRoute($route, $method = 'GET')
{
$method = strtoupper($method);

if (!array_key_exists($method, $this->routes))
{
throw new \InvalidArgumentException(sprintf('%s is not a valid HTTP method.', $method));
}

// Get the path from the route and remove and leading or trailing slash.
$route = trim(parse_url($route, PHP_URL_PATH), ' /');

// Iterate through all of the known routes looking for a match.
foreach ($this->routes[$method] as $rule)
{
if (preg_match($rule['regex'], $route, $matches))
{
// If we have gotten this far then we have a positive match.
$vars = $rule['defaults'];

foreach ($rule['vars'] as $i => $var)
{
$vars[$var] = $matches[$i + 1];
}

$controller = preg_split("/[.]+/", $rule['controller']);

return [
'controller' => $controller[0],
'task' => $controller[0],
'vars' => $vars
];
}
}

throw new \InvalidArgumentException(sprintf('Unable to handle request for route `%s`.', $route), 404);
}
}

0 comments on commit 2e1eec0

Please sign in to comment.