-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup a basic application based on Minz
- Loading branch information
1 parent
03c3cb8
commit 51720fd
Showing
14 changed files
with
238 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<p> | ||
Hello World! | ||
</p> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |