Skip to content

Commit

Permalink
Setup a basic application based on Minz
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed May 8, 2020
1 parent 03c3cb8 commit 51720fd
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/Minz"]
path = lib/Minz
url = https://github.com/flusio/Minz.git
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ stop: ## Stop and clean Docker server
install: ## Install the dependencies
$(COMPOSER) install

.PHONY: setup
setup: .env ## Setup the application system
$(PHP) ./cli --request /system/setup

.PHONY: update
update: setup ## Update the application

.PHONY: test
test: ## Run the test suite
$(PHP) ./vendor/bin/phpunit --coverage-text --whitelist ./src --bootstrap ./tests/bootstrap.php --testdox ./tests
Expand All @@ -38,3 +45,6 @@ lint-fix: ## Fix the errors detected by the linter
.PHONY: help
help:
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.env:
@cp env.sample .env
24 changes: 24 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Configure the autoload of the flusio application and its dependencies.
*
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/

spl_autoload_register(
function ($class_name) {
$app_name = 'flusio';
$app_path = __DIR__;
$lib_path = $app_path . '/lib';

if (strpos($class_name, 'Minz') === 0) {
include $lib_path . '/Minz/autoload.php';
} elseif (strpos($class_name, $app_name) === 0) {
$class_name = substr($class_name, strlen($app_name) + 1);
$class_path = str_replace('\\', '/', $class_name) . '.php';
include $app_path . '/src/' . $class_path;
}
}
);
22 changes: 22 additions & 0 deletions configuration/environment_development.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

$db_host = $dotenv->pop('DB_HOST');
$db_port = $dotenv->pop('DB_PORT');
$db_name = 'flusio_development';

return [
'app_name' => 'flusio',

'secret_key' => $dotenv->pop('APP_SECRET_KEY'),

'url_options' => [
'host' => $dotenv->pop('APP_HOST'),
'port' => intval($dotenv->pop('APP_PORT')),
],

'database' => [
'dsn' => "pgsql:host={$db_host};port={$db_port};dbname={$db_name}",
'username' => $dotenv->pop('DB_USERNAME'),
'password' => $dotenv->pop('DB_PASSWORD'),
],
];
24 changes: 24 additions & 0 deletions configuration/environment_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

$db_host = $dotenv->pop('DB_HOST');
$db_port = $dotenv->pop('DB_PORT');
$db_name = 'flusio_test';

return [
'app_name' => 'flusio',

'secret_key' => $dotenv->pop('APP_SECRET_KEY'),

'url_options' => [
'host' => 'test.flus.io',
'protocol' => 'https',
],

'database' => [
'dsn' => "pgsql:host={$db_host};port={$db_port};dbname={$db_name}",
'username' => $dotenv->pop('DB_USERNAME'),
'password' => $dotenv->pop('DB_PASSWORD'),
],

'no_syslog' => !getenv('APP_SYSLOG_ENABLED'),
];
9 changes: 9 additions & 0 deletions env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
APP_ENVIRONMENT=development
APP_SECRET_KEY=change-me
APP_HOST=localhost
APP_PORT=8000

DB_HOST=database
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
1 change: 1 addition & 0 deletions lib/Minz
Submodule Minz added at a12d64
36 changes: 36 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/

// Setup the Minz framework
$app_path = realpath(__DIR__ . '/..');

include $app_path . '/autoload.php';
\Minz\Configuration::load('dotenv', $app_path);
\Minz\Environment::initialize();
\Minz\Environment::startSession();

// Get the http information and create a Request
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
$http_method = $request_method === 'head' ? 'get' : $request_method;
$http_uri = $_SERVER['REQUEST_URI'];
$http_parameters = array_merge($_GET, $_POST);

$request = new \Minz\Request($http_method, $http_uri, $http_parameters, $_SERVER);

// Initialize the Application and execute the request to get a Response
$application = new \flusio\Application();
$response = $application->run($request);

// Generate the HTTP headers and output
http_response_code($response->code());
foreach ($response->headers() as $header) {
header($header);
}

if ($request_method !== 'head') {
echo $response->render();
}
57 changes: 57 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace flusio;

/**
* This is the central class of the application. It declares routes and
* executes a Request to return a Response.
*
* It is called from the public/index.php file which is the entrypoint from a
* Web browser.
*
* Example:
*
* $request = new \Minz\Request('get', '/');
* $application = new \flusio\Application();
* $response = $application->run($request);
* echo $response->render();
*
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/
class Application
{
/** @var \Minz\Engine **/
private $engine;

/**
* Setup a Router and declare its routes.
*/
public function __construct()
{
// Initialize the routes
$router = new \Minz\Router();
$router->addRoute('get', '/', 'Pages#home', 'home');

$this->engine = new \Minz\Engine($router);
\Minz\Url::setRouter($router);
}

/**
* Declare global View variables and execute a request.
*
* @param \Minz\Request $request
*
* @return \Minz\Response
*/
public function run($request)
{
\Minz\Output\View::declareDefaultVariables([
'environment' => \Minz\Configuration::$environment,
'errors' => [],
'error' => null,
]);

return $this->engine->run($request);
}
}
24 changes: 24 additions & 0 deletions src/Pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace flusio;

use Minz\Response;

/**
* Handle the requests to the static pages of the application.
*
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/
class Pages
{
/**
* Show the home page.
*
* @return \Minz\Response
*/
public function home()
{
return Response::ok('pages/home.phtml');
}
}
3 changes: 3 additions & 0 deletions src/views/pages/home.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>
Hello World!
</p>
15 changes: 0 additions & 15 deletions tests/HelloWorldTest.php

This file was deleted.

19 changes: 19 additions & 0 deletions tests/PagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace flusio;

use Minz\Tests\IntegrationTestCase;

class PagesTest extends IntegrationTestCase
{
public function testHomeRendersCorrectly()
{
$request = new \Minz\Request('GET', '/');

$response = self::$application->run($request);

$this->assertResponse($response, 200, 'Hello World!');
$pointer = $response->output()->pointer();
$this->assertSame('pages/home.phtml', $pointer);
}
}
6 changes: 6 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<?php

$app_path = realpath(__DIR__ . '/..');

include $app_path . '/autoload.php';

\Minz\Configuration::load('test', $app_path);
\Minz\Environment::initialize();
\Minz\Environment::startSession();

0 comments on commit 51720fd

Please sign in to comment.