-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ABGEO07/develop
v1.0.0
- Loading branch information
Showing
4 changed files
with
159 additions
and
4 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,13 @@ | ||
# Cherry-core Changelog | ||
|
||
## [v1.0.0](https://github.com/ABGEO07/cherry-core/releases/tag/v1.0.0 "v1.0.0") (2019-03-29) | ||
#### The first stable version | ||
|
||
- Package available on: `composer require cherry-project/core` | ||
|
||
- Core contains all cherry-project sub-packages | ||
- [Request](https://github.com/ABGEO07/cherry-request) | ||
- [Response](https://github.com/ABGEO07/cherry-response) | ||
- [Router](https://github.com/ABGEO07/cherry-router) | ||
- [Templater](https://github.com/ABGEO07/cherry-templater) | ||
- [Logger](https://github.com/ABGEO07/cherry-logger) |
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,12 +1,41 @@ | ||
<?php | ||
/** | ||
* The file contains ControllerTrait. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Library | ||
* @package Cherry | ||
* @author Temuri Takalandze <[email protected]> | ||
* @license https://github.com/ABGEO07/cherry-core/blob/master/LICENSE MIT | ||
* @link https://github.com/ABGEO07/cherry-core | ||
*/ | ||
|
||
namespace Cherry\Controller; | ||
|
||
use Cherry\HttpUtils\Response; | ||
use Cherry\Templating\Templater; | ||
|
||
/** | ||
* Trait for Cherry Controllers. | ||
* | ||
* @category Library | ||
* @package Cherry | ||
* @author Temuri Takalandze <[email protected]> | ||
* @license https://github.com/ABGEO07/cherry-core/blob/master/LICENSE MIT | ||
* @link https://github.com/ABGEO07/cherry-core | ||
*/ | ||
trait ControllerTrait | ||
{ | ||
private function render($template, $args = []) | ||
/** | ||
* Render the template. | ||
* | ||
* @param string $template Template filename for rendering | ||
* @param array $args Arguments for passing in template | ||
* | ||
* @return Response | ||
*/ | ||
protected function render($template, $args = []) | ||
{ | ||
$te = new Templater(TEMPLATES_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 |
---|---|---|
@@ -1,16 +1,41 @@ | ||
<?php | ||
/** | ||
* The file contains application Kernel class. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Library | ||
* @package Cherry | ||
* @author Temuri Takalandze <[email protected]> | ||
* @license https://github.com/ABGEO07/cherry-core/blob/master/LICENSE MIT | ||
* @link https://github.com/ABGEO07/cherry-core | ||
*/ | ||
|
||
namespace Cherry; | ||
|
||
use Cherry\Routing\Router; | ||
|
||
/** | ||
* Application Kernel class. | ||
* | ||
* @category Library | ||
* @package Cherry | ||
* @author Temuri Takalandze <[email protected]> | ||
* @license https://github.com/ABGEO07/cherry-core/blob/master/LICENSE MIT | ||
* @link https://github.com/ABGEO07/cherry-core | ||
*/ | ||
class Kernel | ||
{ | ||
private $_appRoot; | ||
|
||
public $router; | ||
public $logger; | ||
|
||
/** | ||
* Kernel constructor. | ||
* | ||
* @param string $appRoot Application root path. | ||
*/ | ||
public function __construct($appRoot) | ||
{ | ||
define('__ROOT__', $appRoot); | ||
|
@@ -20,6 +45,11 @@ public function __construct($appRoot) | |
$this->run(); | ||
} | ||
|
||
/** | ||
* Run application. | ||
* | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
$this->_readConfig(); | ||
|
@@ -28,6 +58,11 @@ public function run() | |
$this->logger = new Logger('app', LOGS_PATH); | ||
} | ||
|
||
/** | ||
* Read config from file. | ||
* | ||
* @return array | ||
*/ | ||
private function _getConfig() | ||
{ | ||
$config = file_get_contents(__ROOT__ . '/config/config.json') | ||
|
@@ -36,6 +71,13 @@ private function _getConfig() | |
return json_decode($config, 1); | ||
} | ||
|
||
/** | ||
* Check if needed keys isset in config file | ||
* | ||
* @param array $config Application Config | ||
* | ||
* @return void | ||
*/ | ||
private function _validateConfig(array $config) | ||
{ | ||
$neededKeys = [ | ||
|
@@ -45,21 +87,26 @@ private function _validateConfig(array $config) | |
'LOGS_PATH' | ||
]; | ||
|
||
foreach ($neededKeys as $k) | ||
{ | ||
foreach ($neededKeys as $k) { | ||
if (!isset($config[$k])) { | ||
die("Parameter \"{$k}\" don't found in config file!"); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Define application config values as constants. | ||
* | ||
* @return void | ||
*/ | ||
private function _readConfig() | ||
{ | ||
$config = $this->_getConfig(); | ||
|
||
$this->_validateConfig($config); | ||
|
||
foreach ($config as $k => $v) | ||
foreach ($config as $k => $v) { | ||
define($k, __ROOT__ . '/' . $v); | ||
} | ||
} | ||
} |
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,69 @@ | ||
# Cherry-Core | ||
The Cherry-project Core | ||
|
||
[![GitHub license](https://img.shields.io/github/license/abgeo07/cherry-core.svg)](https://github.com/ABGEO07/cherry-core/blob/master/LICENSE) | ||
|
||
[![GitHub release](https://img.shields.io/github/release/abgeo07/cherry-core.svg)](https://github.com/ABGEO07/cherry-core/releases) | ||
|
||
[![Packagist Version](https://img.shields.io/packagist/v/cherry-project/core.svg "Packagist Version")](https://packagist.org/packages/cherry-core/request "Packagist Version") | ||
|
||
------------ | ||
|
||
This is core for Cherry-Project that contains Cherry [Request](https://github.com/ABGEO07/cherry-request), | ||
[Response](https://github.com/ABGEO07/cherry-response), [Router](https://github.com/ABGEO07/cherry-router), | ||
[Templater](https://github.com/ABGEO07/cherry-templater) and [Logger](https://github.com/ABGEO07/cherry-logger). | ||
|
||
In root of your application you must define main file and call application Core Kernel class: | ||
|
||
```php | ||
<?php | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$kernel = new Cherry\Kernel(__DIR__); | ||
``` | ||
|
||
`Kernel` class takes only one argument - your application root path. | ||
|
||
With main file you mast have directories `config`, `controllers`, `templates`: | ||
|
||
``` | ||
root | ||
└─ config/ | ||
└─ controllers/ | ||
└─ templates/ | ||
``` | ||
|
||
### Config | ||
|
||
`config` directory is a 'home' for application config and router files: | ||
``` | ||
config/ | ||
└─ config.json | ||
└─ routes.json | ||
``` | ||
|
||
`config.json` contains application environmental parameters: | ||
```json | ||
{ | ||
"ROUTES_FILE": "path-to-routes.json", | ||
"CONTROLLERS_PATH": "path-to-controllers-directory", | ||
"TEMPLATES_PATH": "path-to-templates-directory", | ||
"LOGS_PATH": "path-to-logs-directory" | ||
} | ||
``` | ||
|
||
### Controller | ||
|
||
Controller is a simple PHP class that contains public methods mapped on route: | ||
``` | ||
"action": "Cherry\\Controller\\DefaultController::index" | ||
``` | ||
|
||
Every method returns Cherry\HttpUtils\Response object. | ||
|
||
```php | ||
$this->render('index'); | ||
``` | ||
|
||
**2019 © Cherry-project** |