Skip to content

Commit

Permalink
support namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rafageist committed Jul 9, 2019
1 parent 5828ed1 commit 1af74af
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
42 changes: 29 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
Jul 8, 2019
-------------------
- Support namespaces! Now `divengine\ways` detect namespace instruction.

```php
<?php

namespace MyApp;

#listen = /
class MyController {
static function Run() {
echo "Hello universe";
}
}
```
Jul 6, 2019
-------------------
- Adding rules!
Expand All @@ -22,23 +38,23 @@ Jul 3, 2019

Jun 24, 2019
-------------------
- IMPORTAN bugfix! As in divWays, as you are executing, more control points can be loaded depending
- IMPORTAN bugfix! As in divengine\ways, as you are executing, more control points can be loaded depending
on the flow that you have designed for the user's actions, since the list of control points is possibly
a "list that is dynamically increasing".

Solution: Fix `divWays::callAll()` method adding & before item foreach
Solution: Fix `divengine\ways::callAll()` method adding & before item foreach

```php
foreach (self::$__listen as $pattern => &$methods) { ... }
```

Now the expected behavior must happen

- New method `divWays::invoke($way, $data)`
- New method `divengine\ways::invoke($way, $data)`

```php
<?php
$entry = divWays::invoke('model://entries/1', [
$entry = divengine\ways::invoke('model://entries/1', [
'filter' => 'monkey island'
]);
```
Expand All @@ -65,13 +81,13 @@ _index.php_

```php
<?php
include "divWays.php"
include "divengine\ways.php"

divWays::listen("/my/way", function($data, $args) {
divengine\ways::listen("/my/way", function($data, $args) {
echo "Hello world";
});

divWays::bootstrap('_url'); // $_GET['_url'] is missing, then assume $_GET['_url'] = $_SERVER['REQUEST_URI']
divengine\ways::bootstrap('_url'); // $_GET['_url'] is missing, then assume $_GET['_url'] = $_SERVER['REQUEST_URI']
```

_cli_
Expand Down Expand Up @@ -123,13 +139,13 @@ _bootstrap.php_
```php
<?php

include "divWays.php";
divWays::register("control/Blog.php");
include "divengine\ways.php";
divengine\ways::register("control/Blog.php");
```

Oct 14, 2017
-------------------
- New method divWays::redirect($way)
- New method divengine\ways::redirect($way)

Oct 11, 2017
-------------------
Expand All @@ -143,7 +159,7 @@ Sep 20, 2017
<?php

// documentation/chapter-1
divWays::listen("documentation/chapter-{id|is_int}", function($data, $args){
divengine\ways::listen("documentation/chapter-{id|is_int}", function($data, $args){
echo "Chapter #{$args['id']}";
});

Expand All @@ -159,7 +175,7 @@ Sep 18, 2017

```php
<?php
divWays::listen("blog/{id:is_int}", function($data, $args){
divengine\ways::listen("blog/{id:is_int}", function($data, $args){
echo "{$args['id']} is integer";
});
```
Expand All @@ -175,7 +191,7 @@ Sep 17, 2017
```php
<?php

divWays::listen("blog/{id}", function($data, $args){
divengine\ways::listen("blog/{id}", function($data, $args){
echo "Entry #{$args['id']}";
});
```
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Div PHP Ways 2.1.0
# Div PHP Ways 2.2.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 Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"controllers"
],
"homepage": "https://divengine.com/ways",
"version": "2.1.0",
"version": "2.2.0",
"require": {
"php": ">=5.4.0",
"ext-json": "*"
Expand Down
52 changes: 32 additions & 20 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.1.0
* @version 2.2.0
*
* @link https://divengine.com
* @link https://divengine.com/ways
Expand Down Expand Up @@ -55,7 +55,7 @@ class ways

const PROPERTY_RULES = 'rules';

private static $__version = '2.1.0';
private static $__version = '2.2.0';

private static $__way_var = null;

Expand Down Expand Up @@ -932,7 +932,7 @@ static function call($controller, $data = [], $args = [], &$output = '', $show_o
}

$method = "{$controller}@{$action}";
if (isset($rules[$method])){
if (isset($rules[$method])) {
$rules = $rules[$method];
if (is_string($rules)) {
$rules = [$rules];
Expand Down Expand Up @@ -1244,16 +1244,16 @@ static function listen($way, $controller, $properties = [])
* @param string $path
* @param array $properties
*/
static function register($path, $properties = [])
public static function register($path, $properties = [])
{
if (!file_exists($path) && file_exists(PACKAGES."$path")) {
$path = PACKAGES.$path;
}

$namespace = null;
$prop = self::getCodeProperties($path, '#', $namespace);
$class_name = self::getClassName($path);

$prop = self::getCodeProperties($path);

$class_name = $namespace === null ? $class_name : "$namespace\\$class_name";
$prop = self::cop($prop, $properties);

if (!isset($prop[self::PROPERTY_ID])) {
Expand All @@ -1280,20 +1280,21 @@ static function register($path, $properties = [])
$action = $prop[self::PROPERTY_ID].'@'.$method;

$rules = [];
if (isset($prop["rules@{$method}"]))
{
if (isset($prop["rules@{$method}"])) {
$rules = $prop["rules@{$method}"];
if (!is_array($rules)) $rules = [$rules];
foreach ($rules as $rule)
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]))
{
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]];
}

self::$__controllers[$prop[self::PROPERTY_ID]]['prop'][self::PROPERTY_RULES][$action][] = $rule;
}
}
}

foreach ($prop[$key] as $way) {
Expand All @@ -1311,13 +1312,15 @@ static function register($path, $properties = [])
}

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

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


Expand All @@ -1339,7 +1342,6 @@ static function getClassName($path)
$class_name = explode("/", $path);
$class_name = $class_name[count($class_name) - 1];
$class_name = str_replace('.php', '', $class_name);

return $class_name;
}

Expand All @@ -1348,25 +1350,35 @@ static function getClassName($path)
*
* @param string $path
* @param string $prefix
* @param string $namespace
*
* @return array
*/
static function getCodeProperties($path, $prefix = '#')
static function getCodeProperties($path, $prefix, &$namespace = null)
{
if (!file_exists($path)) {
return [];
}

$f = fopen($path, "r");
$f = fopen($path, 'r');

$property_value = null;

$l = strlen($prefix);
$prop = [];
$namespace = null;

while (!feof($f)) {
$s = fgets($f);
$s = trim($s);

// detect namespace
$ss = strtolower(trim($s));

if ($namespace === null && strpos($ss, 'namespace ') === 0) {
$namespace = trim(substr($s, 9, -1));
}

if (strtolower(substr($s, 0, $l)) == strtolower($prefix)) {
$s = substr($s, $l);
$s = trim($s);
Expand Down

0 comments on commit 1af74af

Please sign in to comment.