Skip to content

Commit

Permalink
Pre-release update
Browse files Browse the repository at this point in the history
  • Loading branch information
ilhooq committed Sep 25, 2021
1 parent a042030 commit 7856af8
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 106 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ applications.

## specifications

- Lightweight: It requires no composer dependencies and its code base is under 100kb.
- Fast: Basic routing, components lasy loading and uses PHP as template engine.
- Customizable: The framework can be extended throw Event hooks and behavior injections.
- Lightweight: Code base including its dependencies is under 100kb.
- Blazing fast: fast router, components lasy loading and uses PHP as template engine.
- Customizable: The framework components can be extended throw Event hooks and behavior injections.
- Stable: The framework components have been well tested and the API is fixed.
- Modular : internal routes correspond to MVC modules.

Expand All @@ -20,9 +20,6 @@ applications.
- Router: resolve url to routes and get url from routes.
- Base Classes for controllers and models.
- Views : Layout management and themable views.
- I18n : To internatinalize applications.
- User session management: user login / logout and user permissions.
- Assets management : To use external assets in your project

## Installation via composer

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"require-dev" : {
"phpunit/phpunit" : "^9.5",
"squizlabs/php_codesniffer" : "^3.5"
"squizlabs/php_codesniffer" : "^3.5",
"phpstan/phpstan": "^0.12.99"
},
"scripts" : {
"test": [
Expand Down
47 changes: 36 additions & 11 deletions src/Application.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

/**
* This file is part of Piko - Web micro framework
*
* @copyright 2019-2021 Sylvain PHILIP
* @license LGPL-3.0; see LICENSE.txt
* @link https://github.com/piko-framework/piko
*/

declare(strict_types=1);

namespace piko;
Expand Down Expand Up @@ -50,7 +52,7 @@ class Application extends Component
* ]
* ```
*
* @var array
* @var array<mixed>
* @see Module To have more informations on module attributes
*/
public $modules = [];
Expand All @@ -63,7 +65,7 @@ class Application extends Component
* During the bootstrapping process, each module will be instantiated. If the module class
* implements the bootstrap() method, this method will be also be called.
*
* @var array
* @var array<string>
*/
public $bootstrap = [];

Expand All @@ -77,7 +79,7 @@ class Application extends Component
/**
* The configuration loaded on application instantiation.
*
* @var array
* @var array<mixed>
*/
public $config = [];

Expand Down Expand Up @@ -114,14 +116,21 @@ class Application extends Component
/**
* The response headers.
*
* @var array
* @var array<string>
*/
public $headers = [];

/**
* Application Instance
*
* @var Application
*/
public static $instance;

/**
* Constructor
*
* @param array $config The application configuration.
* @param array<mixed> $config The application configuration.
* @return void
*/
public function __construct(array $config)
Expand Down Expand Up @@ -153,19 +162,29 @@ public function __construct(array $config)
}
}

$baseUrl = isset($config['baseUrl'])? $config['baseUrl'] : rtrim(dirname($_SERVER['SCRIPT_NAME']), '\\/');
$baseUrl = isset($config['baseUrl']) ? $config['baseUrl'] : rtrim(dirname($_SERVER['SCRIPT_NAME']), '\\/');

Piko::setAlias('@web', $baseUrl);
Piko::setAlias('@webroot', dirname($_SERVER['SCRIPT_FILENAME']));
Piko::setAlias('@app', $this->basePath);

$this->config = $config;

Piko::$app = $this;
static::$instance = $this;

$this->trigger('init');
}

/**
* Get the application instance
*
* @return Application
*/
public static function getInstance(): Application
{
return static::$instance;
}

/**
* Run the application.
*
Expand Down Expand Up @@ -245,7 +264,7 @@ public function dispatch($route)
if ($module->layout !== false) {
$layout = $module->layout == null ? $this->defaultLayout : $module->layout;
$view = $this->getView();
$path = empty($module->layoutPath)? $this->defaultLayoutPath : $module->layoutPath;
$path = empty($module->layoutPath) ? $this->defaultLayoutPath : $module->layoutPath;
$view->paths[] = $path;
$output = $view->render($layout, ['content' => $output]);
}
Expand All @@ -265,10 +284,10 @@ public function dispatch($route)
* @param string $header The complete header (key:value) or just the header key
* @param string $value (optional) The header value
*/
public function setHeader(string $header, string $value = null) : void
public function setHeader(string $header, string $value = ''): void
{
if (($pos = strpos($header, ':')) !== false) {
$value = substr($header, $pos +1);
$value = substr($header, $pos + 1);
$header = substr($header, 0, $pos);
}

Expand Down Expand Up @@ -309,6 +328,12 @@ public function getModule($moduleId)
throw new RuntimeException("Configuration not found for module {$moduleId}.");
}

return Piko::createObject($this->modules[$moduleId]);
$module = Piko::createObject($this->modules[$moduleId]);

if ($module instanceof Module) {
return $module;
}

throw new RuntimeException("module $moduleId must be instance of Module");
}
}
54 changes: 29 additions & 25 deletions src/Controller.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

/**
* This file is part of Piko - Web micro framework
*
* @copyright 2019-2021 Sylvain PHILIP
* @license LGPL-3.0; see LICENSE.txt
* @link https://github.com/piko-framework/piko
*/

declare(strict_types=1);

namespace piko;
Expand All @@ -15,6 +17,9 @@
/**
* Controller is the base class for classes containing controller logic.
*
* @method string getUrl(string $route, array<mixed> $params, boolean $absolute) Convenient method to convert
* a route to an url (see Router::getUrl()). This method is implemented as a behavior and can be overriden.
*
* @author Sylvain PHILIP <contact@sphilip.com>
*/
abstract class Controller extends Component
Expand Down Expand Up @@ -51,6 +56,13 @@ abstract class Controller extends Component
*/
public $module;

protected function init(): void
{
if (!isset($this->behaviors['getUrl'])) {
$this->behaviors['getUrl'] = [Application::getInstance()->getRouter(), 'getUrl'];
}
}

/**
* Runs an action within this controller with the specified action ID.
Expand Down Expand Up @@ -78,12 +90,12 @@ public function runAction(string $id)
* Render a view.
*
* @param string $viewName The view file name.
* @param array $data An array of data (name-value pairs) to transmit to the view.
* @param array<mixed> $data An array of data (name-value pairs) to transmit to the view.
* @return string|null The view output.
*/
protected function render(string $viewName, array $data = []): ?string
{
$view = Piko::$app->getView();
$view = Application::getInstance()->getView();
$view->paths[] = $this->getViewPath();

return $view->render($viewName, $data);
Expand All @@ -96,7 +108,7 @@ protected function render(string $viewName, array $data = []): ?string
*/
protected function redirect(string $url): void
{
Piko::$app->setHeader('Location', $url);
Application::getInstance()->setHeader('Location', $url);
}

/**
Expand All @@ -106,21 +118,7 @@ protected function redirect(string $url): void
*/
protected function forward(string $route): string
{
return Piko::$app->dispatch($route);
}

/**
* Convenient method to convert a route to an url
*
* @param string $route The route to convert
* @param array $params The route params
* @param boolean $absolute Optional to have an absolute url.
* @return string
* @see Router::getUrl
*/
protected function getUrl(string $route, array $params = [], bool $absolute = false): string
{
return Piko::$app->getRouter()->getUrl($route, $params, $absolute);
return Application::getInstance()->dispatch($route);
}

/**
Expand All @@ -147,8 +145,10 @@ protected function getViewPath()
*/
protected function isAjax(): bool
{
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
if (
isset($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
return true;
}

Expand Down Expand Up @@ -209,13 +209,17 @@ protected function isDelete(): bool
* Get the raw input data of the request
*
* @param int $size The size in bytes of the raw input
* @return string
* @return string|false
*/
protected function rawInput($size = 1024)
{
$handle = fopen('php://input', 'r');
$data = fread($handle, $size);
fclose($handle);
$data = false;

if (is_resource($handle)) {
$data = fread($handle, $size);
fclose($handle);
}

return $data;
}
Expand All @@ -224,12 +228,12 @@ protected function rawInput($size = 1024)
* Convenient method to return a JSON response
*
* @param mixed $data
* @return string
* @return string|false
*/
protected function jsonResponse($data)
{
$this->layout = false;
Piko::$app->setHeader('Content-Type', 'application/json');
Application::getInstance()->setHeader('Content-Type', 'application/json');

return json_encode($data);
}
Expand Down
8 changes: 5 additions & 3 deletions src/HttpException.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

/**
* This file is part of Piko - Web micro framework
*
* @copyright 2019-2021 Sylvain PHILIP
* @license LGPL-3.0; see LICENSE.txt
* @link https://github.com/piko-framework/piko
*/

declare(strict_types=1);

namespace piko;
Expand All @@ -28,14 +30,14 @@ class HttpException extends Exception
* @param int $code The exception code (should be an HTTP status code, eg. 404)
* @param Throwable $previous A previous exception.
*/
public function __construct(string $message = null, int $code = null, Throwable $previous = null)
public function __construct(string $message = '', int $code = 404, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);

if ($this->getCode()) {
$protocol = isset($_SERVER['SERVER_PROTOCOL'])? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
$header = $protocol . ' ' . $this->getCode() . ' ' . $this->getMessage();
array_unshift(Piko::$app->headers, $header);
array_unshift(Application::$instance->headers, $header);
}
}
}

0 comments on commit 7856af8

Please sign in to comment.