Skip to content

Commit

Permalink
ways rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rafageist committed Jul 6, 2019
1 parent 4f7aacd commit af9fa82
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 23 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
Jul 6, 2019
-------------------
- Adding rules!

```php

ways::rule('some-rule', function() {return true or false;});

ways::listen("/secret", function() {...}, [
ways::PROPERTY_RULES => [
'some-rule',
function () {
return true; // another rule
}
]
]);

```
Jul 3, 2019
-------------------
- Structural changes: `divengine` namespace and `ways` classname
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Div PHP Ways 2.0.0
# Div PHP Ways 2.1.0

A "way" is different to a "route". We need a path for found
a specific resource, but we need a way for do something.
Expand All @@ -21,8 +21,6 @@ of a URL may suggest that Div Ways allows a hierarchical
structure of contorl points, but it does not, it can
create a whole graph structure.

![div ways MVC](https://github.com/divengine/resources/raw/master/div-ways/cards/div-ways-mvc-sample.png)

In addition to this, a control point may require the
previous execution of another control point. You can also
implement events or hooks, so you can execute one control
Expand All @@ -49,7 +47,7 @@ With composer...
composer require divengine/ways
```

Without composer, donwload the class and...
Without composer, download the class and...

```php
include "path/to/divengine/ways.php";
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"controllers"
],
"homepage": "https://divengine.com/ways",
"version": "2.0.0",
"version": "2.1.0",
"require": {
"php": ">=5.4.0"
"php": ">=5.4.0",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
145 changes: 128 additions & 17 deletions src/ways.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @package divengine/ways
* @author Rafa Rodriguez [@rafageist] <[email protected]>
* @version 2.0.0
* @version 2.1.0
*
* @link https://divengine.com
* @link https://divengine.com/ways
Expand All @@ -35,6 +35,7 @@
define('DIV_WAYS_BEFORE_RUN', 2);
define('DIV_WAYS_BEFORE_OUTPUT', 3);
define('DIV_WAYS_AFTER_RUN', 4);
define('DIV_WAYS_RULE_FALSE', 'rule_false');

class ways
{
Expand All @@ -50,7 +51,11 @@ class ways

const DEFAULT_WAY_VAR = DIV_WAYS_DEFAULT_WAY_VAR;

private static $__version = '2.0.0';
const PROPERTY_ID = 'id';

const PROPERTY_RULES = 'rules';

private static $__version = '2.1.0';

private static $__way_var = null;

Expand All @@ -76,6 +81,8 @@ class ways

private static $__is_cli = null;

private static $__rules = [];

/**
* Get current version
*
Expand Down Expand Up @@ -416,7 +423,7 @@ static function callAll($way, &$output = '', $show_output = true, $request_metho
$request_methods[$method] = $method;
}

if (!is_null($request_method)) {
if ($request_method !== null) {
$request_methods[$request_method] = $request_method;
}

Expand Down Expand Up @@ -899,7 +906,7 @@ static function call($controller, $data = [], $args = [], &$output = '', $show_o
$action = 'Run';

$ignore_properties = false;
if (stripos($controller, '@')) {
if (strpos($controller, '@')) {
$arr = explode('@', $controller);
$controller = $arr[0];
$action = $arr[1];
Expand All @@ -915,6 +922,38 @@ static function call($controller, $data = [], $args = [], &$output = '', $show_o
self::$__done[$original_controller] = true;

$control = self::$__controllers[$controller];

// check rules
if (array_key_exists(self::PROPERTY_RULES, $control['prop'])) {
$rules = $control['prop'][self::PROPERTY_RULES];

if (is_string($rules)) {
$rules = [$rules];
}

$method = "{$controller}@{$action}";
if (isset($rules[$method])){
$rules = $rules[$method];
if (is_string($rules)) {
$rules = [$rules];
}
}

foreach ($rules as $rule) {
$check = true;
if (is_string($rule) && array_key_exists($rule, self::$__rules)) {
$check = self::checkRule($rule);
} elseif (is_callable($rule)) {
$check = $rule();
}

if ($check === false) // prevent execution
{
return DIV_WAYS_RULE_FALSE;
}
}
}

$class_name = $control['class_name'];

if (!$ignore_properties) {
Expand Down Expand Up @@ -1129,7 +1168,7 @@ static function parseWay($way)
if (!isset($url['path'])) {
$url['path'] = '';
}
if (substr($url['host'], 0, 1) == "/") {
if (substr($url['host'], 0, 1) === "/") {
$url['host'] = substr($url['host'], 1);
}
if (substr($url['path'], 0, 1) == "/") {
Expand All @@ -1147,16 +1186,21 @@ static function parseWay($way)
*
* @param string $way
* @param string $controller
* @param array $properties
* @param mixed $properties
*
* @return string
*/
static function listen($way, $controller, $properties = [])
{
$way = self::parseWay($way);

if (!isset($properties['id'])) {
$properties['id'] = uniqid("closure-");
// $properties is the ID when is a string
if (is_string($properties)) {
$properties = [self::PROPERTY_ID => $properties];
}

if (!isset($properties[self::PROPERTY_ID])) {
$properties[self::PROPERTY_ID] = uniqid("closure-");
}

if (!isset($properties['type'])) {
Expand All @@ -1176,22 +1220,22 @@ static function listen($way, $controller, $properties = [])
}

if (is_callable($controller) && !is_string($controller)) {
self::$__controllers[$properties['id']] = [
self::$__controllers[$properties[self::PROPERTY_ID]] = [
'class_name' => null,
'prop' => $properties,
'path' => null,
'is_closure' => true,
'closure' => $controller,
];

$controller = $properties['id'];
$controller = $properties[self::PROPERTY_ID];
}

foreach ($way['methods'] as $request_method) {
self::$__listen[$way['way']][$request_method][] = $controller;
}

return $properties['id'];
return $properties[self::PROPERTY_ID];
}

/**
Expand All @@ -1212,11 +1256,11 @@ static function register($path, $properties = [])

$prop = self::cop($prop, $properties);

if (!isset($prop['id'])) {
$prop['id'] = $path;
if (!isset($prop[self::PROPERTY_ID])) {
$prop[self::PROPERTY_ID] = $path;
}

self::$__controllers[$prop['id']] = [
self::$__controllers[$prop[self::PROPERTY_ID]] = [
'class_name' => $class_name,
'path' => $path,
'prop' => $prop,
Expand All @@ -1226,14 +1270,31 @@ static function register($path, $properties = [])

// other listeners (by method)
foreach ($prop as $key => $value) {
if (substr($key, 0, 7) == 'listen@') {
if (strpos($key, 'listen@') === 0) {
if (!is_array($prop[$key])) {
$prop[$key] = [
$prop[$key],
];
}
$method = trim(substr($key.' ', 7));
$action = $prop[self::PROPERTY_ID].'@'.$method;

$rules = [];
if (isset($prop["rules@{$method}"]))
{
$rules = $prop["rules@{$method}"];
if (!is_array($rules)) $rules = [$rules];
foreach ($rules as $rule)
if (!empty(self::$__controllers[$prop[self::PROPERTY_ID]])) {

if (is_string(self::$__controllers[$prop[self::PROPERTY_ID]]['prop'][self::PROPERTY_RULES]))
{
self::$__controllers[$prop[self::PROPERTY_ID]]['prop'][self::PROPERTY_RULES] = [self::$__controllers[$prop[self::PROPERTY_ID]]['prop'][self::PROPERTY_RULES]];
}

$action = $prop['id'].'@'.trim(substr($key.' ', 7));
self::$__controllers[$prop[self::PROPERTY_ID]]['prop'][self::PROPERTY_RULES][$action][] = $rule;
}
}

foreach ($prop[$key] as $way) {
self::listen($way, $action);
Expand All @@ -1249,8 +1310,19 @@ static function register($path, $properties = [])
];
}

$rules = [];
if (isset($prop["rules"]))
{
$rules = $prop["rules"];

if (!is_array($rules)) $rules = [$rules];
foreach ($rules as $rule)
self::$__controllers[$prop[self::PROPERTY_ID]]['prop'][self::PROPERTY_RULES]['Run'][] = $rule;
}


foreach ($prop['listen'] as $way) {
self::listen($way, $prop['id']);
self::listen($way, $prop[self::PROPERTY_ID]);
}
}
}
Expand Down Expand Up @@ -1480,6 +1552,31 @@ static function redirect($way)
exit();
}

/**
* Define a rule
*
* @param $ruleName
* @param $rule
*/
public static function rule($ruleName, $rule)
{
self::$__rules[$ruleName] = $rule;
}

/**
* Check a rule
*
* @param $ruleName
*
* @return bool
*/
public static function checkRule($ruleName)
{
$rule = self::$__rules[$ruleName];

return (bool)$rule();
}

/**
* Return true if the script was executed in the CLI environment
*
Expand All @@ -1494,4 +1591,18 @@ final public static function isCli()
return self::$__is_cli;
}

// ----------------- UTILS -----------------------

/**
* Output a JSON REST response
*
* @param $data
* @param int $http_response_code
*/
public static function rest($data, $http_response_code = 200)
{
header('Content-type: application/json', true, $http_response_code);
http_response_code($http_response_code);
echo json_encode($data);
}
}

0 comments on commit af9fa82

Please sign in to comment.