Skip to content

Commit

Permalink
Add an interface for the Application.
Browse files Browse the repository at this point in the history
I wasn't too happy about the coupling between Console and Http, and this
new interface lets us not have that. Instead Http and Console will
depend on Core (which they already do). I contemplated adding
a ConsoleApplication and HttpApplication interface as well, but thought
that might be a bit much.
  • Loading branch information
markstory committed Jul 7, 2017
1 parent 3a52dfc commit 955885a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/Console/CommandRunner.php
Expand Up @@ -20,7 +20,7 @@
use Cake\Console\Exception\StopException;
use Cake\Console\Shell;
use Cake\Event\EventManagerTrait;
use Cake\Http\BaseApplication;
use Cake\Core\ApplicationInterface;
use Cake\Shell\HelpShell;
use Cake\Shell\VersionShell;
use RuntimeException;
Expand All @@ -35,7 +35,7 @@ class CommandRunner
/**
* The application console commands are being run for.
*
* @var \Cake\Http\BaseApplication
* @var \Cake\Core\ApplicationInterface
*/
protected $app;

Expand All @@ -56,10 +56,10 @@ class CommandRunner
/**
* Constructor
*
* @param \Cake\Http\BaseApplication $app The application to run CLI commands for.
* @param \Cake\Core\ApplicationInterface $app The application to run CLI commands for.
* @param string $root The root command name to be removed from argv.
*/
public function __construct(BaseApplication $app, $root = 'cake')
public function __construct(ApplicationInterface $app, $root = 'cake')
{
$this->app = $app;
$this->root = $root;
Expand Down
56 changes: 56 additions & 0 deletions src/Core/ApplicationInterface.php
@@ -0,0 +1,56 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.5.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Core;

/**
* An interface defining the methods that the
* console runner/http server depend on.
*/
interface ApplicationInterface
{
/**
* Load all the application configuration and bootstrap logic.
*
* Override this method to add additional bootstrap logic for your application.
*
* @return void
*/
public function bootstrap();

/**
* Define the routes for an application.
*
* Use the provided RouteBuilder to define an application's routing.
*
* @param \Cake\Routing\RouteBuilder $routes A route builder to add routes into.
* @return void
*/
public function routes($routes);

/**
* Define the console commands for an application.
*
* @param \Cake\Console\CommandCollection $commands The CommandCollection to add commands into.
* @return \Cake\Console\CommandCollection The updated collection.
*/
public function console($commands);

/**
* Define the HTTP middleware layers for an application.
*
* @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to set in your App Class
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware($middleware);
}
11 changes: 4 additions & 7 deletions src/Http/BaseApplication.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Http;

use Cake\Core\ApplicationInterface;
use Cake\Routing\DispatcherFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -25,7 +26,7 @@
* and ensuring that middleware is attached. It is also invoked as the last piece
* of middleware, and delegates request/response handling to the correct controller.
*/
abstract class BaseApplication
abstract class BaseApplication implements ApplicationInterface
{

/**
Expand All @@ -50,19 +51,15 @@ public function __construct($configDir)
abstract public function middleware($middleware);

/**
* Load all the application configuration and bootstrap logic.
*
* Override this method to add additional bootstrap logic for your application.
*
* @return void
* {@inheritDoc}
*/
public function bootstrap()
{
require_once $this->configDir . '/bootstrap.php';
}

/**
* Define the routes for an application.
* {@inheritDoc}
*
* By default this will load `config/routes.php` for ease of use and backwards compatibility.
*
Expand Down
13 changes: 7 additions & 6 deletions src/Http/Server.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Http;

use Cake\Core\ApplicationInterface;
use Cake\Event\EventDispatcherTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -29,7 +30,7 @@ class Server
use EventDispatcherTrait;

/**
* @var \Cake\Http\BaseApplication
* @var \Cake\Core\ApplicationInterface
*/
protected $app;

Expand All @@ -41,9 +42,9 @@ class Server
/**
* Constructor
*
* @param \Cake\Http\BaseApplication $app The application to use.
* @param \Cake\Core\ApplicationInterface $app The application to use.
*/
public function __construct(BaseApplication $app)
public function __construct(ApplicationInterface $app)
{
$this->setApp($app);
$this->setRunner(new Runner());
Expand Down Expand Up @@ -108,10 +109,10 @@ public function emit(ResponseInterface $response, EmitterInterface $emitter = nu
/**
* Set the application.
*
* @param BaseApplication $app The application to set.
* @param Cake\Core\ApplicationInterface $app The application to set.
* @return $this
*/
public function setApp(BaseApplication $app)
public function setApp(ApplicationInterface $app)
{
$this->app = $app;

Expand All @@ -121,7 +122,7 @@ public function setApp(BaseApplication $app)
/**
* Get the current application.
*
* @return \Cake\Http\BaseApplication The application that will be run.
* @return \Cake\Core\ApplicationInterface The application that will be run.
*/
public function getApp()
{
Expand Down

0 comments on commit 955885a

Please sign in to comment.