Skip to content

Commit

Permalink
Merge pull request #1 from ABGEO07/develop
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
ABGEO authored Mar 29, 2019
2 parents cfb1d25 + 752fdd6 commit cdb99d9
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 4 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
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)
31 changes: 30 additions & 1 deletion Cherry/Controller/ControllerTrait.php
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);

Expand Down
53 changes: 50 additions & 3 deletions Cherry/Kernel.php
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);
Expand All @@ -20,6 +45,11 @@ public function __construct($appRoot)
$this->run();
}

/**
* Run application.
*
* @return void
*/
public function run()
{
$this->_readConfig();
Expand All @@ -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')
Expand All @@ -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 = [
Expand All @@ -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);
}
}
}
66 changes: 66 additions & 0 deletions README.md
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 &copy; Cherry-project**

0 comments on commit cdb99d9

Please sign in to comment.