Skip to content

Commit

Permalink
Added routing to the app (#16)
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
  • Loading branch information
muhakh authored and wilsonge committed Jul 4, 2017
1 parent 3a999ee commit 40ef3a5
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 4 deletions.
3 changes: 2 additions & 1 deletion libraries/joomla/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ protected static function createContainer()
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Document)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Menu)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Session)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Toolbar);
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Toolbar),
->registerServiceProvider(new \Joomla\CMS\Service\Provider\ApiRouter);

return $container;
}
Expand Down
38 changes: 35 additions & 3 deletions libraries/src/CMS/Application/ApiApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ final class ApiApplication extends CMSApplication
*
* @since __DEPLOY_VERSION__
*/

public function __construct(\JInput $input = null, Registry $config = null, WebClient $client = null, Container $container = null)
{
// Register the application name
$this->name = 'japi';

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

// Set format to JSON (uses JDocumentJson)
Expand Down Expand Up @@ -134,5 +133,38 @@ 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 __DEPLOY_VERSION__
*/
protected function route()
{
$uri = \JUri::getInstance();
$router = static::getRouter();

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

/**
* Returns the application Router object.
*
* @return ApiRouter
*
* @since __DEPLOY_VERSION__
*/
public static function getApiRouter()
{
return JFactory::getContainer()->get('ApiRouter');
}
}
90 changes: 90 additions & 0 deletions libraries/src/CMS/Router/ApiRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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\Router;
use Joomla\Router\Router;

/**
* Joomla! API Router class
*
* @since __DEPLOY_VERSION__
*/
class ApiRouter extends Router
{
/**
* Router instances container.
*
* @var array
* @since __DEPLOY_VERSION__
*/
protected static $instances = array();

/**
* Creates routes map for CRUD
*
* @param string $baseName The base name of the component.
* @param string $controller The name of the controller that contains CRUD functions.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function createCRUDRoutes($baseName, $controller, $defaults = array())
{
$routes = array(
array(
'method' => 'GET',
'pattern' => $baseName,
'controller' => $controller . '.display',
'defaults' => $defaults
),
array(
'method' => 'GET',
'pattern' => $baseName . '/new',
'controller' => $controller . '.add',
'defaults' => $defaults
),
array(
'method' => 'GET',
'pattern' => $baseName . '/:id',
'controller' => $controller . '.display',
'rules' => array('id' => '(\d+)'),
'defaults' => $defaults
),
array(
'method' => 'GET',
'pattern' => $baseName . '/:id/edit',
'controller' => $controller . '.edit',
'rules' => array('id' => '(\d+)'),
'defaults' => $defaults
),
array(
'method' => 'POST',
'pattern' => $baseName,
'controller' => $controller . '.add',
'defaults' => $defaults
),
array(
'method' => 'PUT',
'pattern' => $baseName . '/:id',
'controller' => $controller . '.edit',
'rules' => array('id' => '(\d+)'),
'defaults' => $defaults
),
array(
'method' => 'DELETE',
'pattern' => $baseName . '/:id',
'controller' => $controller . '.delete',
'rules' => array('id' => '(\d+)'),
'defaults' => $defaults
),
);
$this->addRoutes($routes);
}
}
45 changes: 45 additions & 0 deletions libraries/src/CMS/Service/Provider/ApiRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Service\Provider;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Router\ApiRouter;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

/**
* Service provider for the application's API router dependency
*
* @since __DEPLOY_VERSION__
*/
class ApiRouter implements ServiceProviderInterface
{
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
{
$container->alias('ApiRouter', 'Joomla\CMS\Router\ApiRouter')
->share(
'Joomla\CMS\Router\ApiRouter',
function (Container $container)
{
return new ApiRouter;
},
true
);
}
}

0 comments on commit 40ef3a5

Please sign in to comment.