From 1af74af8adc9df51c14cbf43b421f85a9ed0a2eb Mon Sep 17 00:00:00 2001 From: rafageist Date: Mon, 8 Jul 2019 18:44:57 -0700 Subject: [PATCH] support namespaces --- CHANGELOG.md | 42 ++++++++++++++++++++++++++++------------- README.md | 2 +- composer.json | 2 +- src/ways.php | 52 +++++++++++++++++++++++++++++++-------------------- 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6ebaa3..2702c14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +Jul 8, 2019 +------------------- +- Support namespaces! Now `divengine\ways` detect namespace instruction. + +```php + &$methods) { ... } @@ -34,11 +50,11 @@ 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 'monkey island' ]); ``` @@ -65,13 +81,13 @@ _index.php_ ```php =5.4.0", "ext-json": "*" diff --git a/src/ways.php b/src/ways.php index e5f9f3f..73b0ab1 100644 --- a/src/ways.php +++ b/src/ways.php @@ -19,7 +19,7 @@ * * @package divengine/ways * @author Rafa Rodriguez [@rafageist] - * @version 2.1.0 + * @version 2.2.0 * * @link https://divengine.com * @link https://divengine.com/ways @@ -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; @@ -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]; @@ -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])) { @@ -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) { @@ -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; + } } @@ -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; } @@ -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);