Skip to content

Commit

Permalink
drop support for old version of php and laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed May 6, 2024
1 parent e30315b commit 3a054c5
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check_imports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress
run: composer install --prefer-dist --no-progress && composer require imanghafoori/php-imports-analyzer --dev

- name: Check Imports
run: ./vendor/bin/check_imports
12 changes: 4 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@
}
],
"require": {
"php": "7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*|8.4.*",
"laravel/framework": "~5.1|6.*|7.*|8.*|9.*|10.*|^11.0|^12.0"
"php": "7.4.*|8.0.*|8.1.*|8.2.*|8.3.*|8.4.*",
"laravel/framework": "~5.5|6.*|7.*|8.*|9.*|10.*|^11.0|^12.0"
},
"require-dev": {
"orchestra/testbench": "~6.0|^9.0",
"squizlabs/php_codesniffer": "3.*",
"imanghafoori/php-imports-analyzer": "^1.0.6"
"squizlabs/php_codesniffer": "3.*"
},
"autoload": {
"psr-4": {
"Imanghafoori\\Decorator\\": "src"
},
"files": [
"src/helpers.php"
]
}
},
"autoload-dev": {
"classmap": [
Expand Down
53 changes: 43 additions & 10 deletions src/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace Imanghafoori\Decorator;

use Illuminate\Support\Str;
use Illuminate\Container\Container;
use ReflectionFunction;
use ReflectionMethod;

class Decorator
{
/**
* All of the decorators for method calls.
* All the decorators for method calls.
*
* @var array
*/
protected $globalDecorators = [];

/**
* All of the decorator names and definitions.
* All the decorator names and definitions.
*
* @var array
*/
Expand Down Expand Up @@ -76,7 +78,7 @@ public function call($callback, array $parameters = [], $defaultMethod = null)
$callback = $this->decorateWith($callback, $decorators);
$parameters = $this->getCallParams($callback, $parameters);

return app()->call($callback, $parameters, $defaultMethod);
return Container::getInstance()->call($callback, $parameters, $defaultMethod);
}

public function unDecorate($decorated, $decorator = null)
Expand All @@ -97,21 +99,22 @@ private function normalizeMethod($callback)

/**
* @param $callable
* @param $decorators
* @param array $decorators
* @return mixed
* @throws \ReflectionException
*/
public function decorateWith($callable, array $decorators)
{
foreach ($decorators as $decorator) {
if (is_string($decorator) and ! Str::contains($decorator, '@')) {
if (is_string($decorator) and ! self::contains($decorator, '@')) {
$decorator = $this->globalDecorators[$decorator];
}

is_array($decorator)
? $params = $this->getCallParams($this->normalizeMethod($decorator), [$callable])
: $params = $this->getCallParams($decorator, [$callable]);

$callable = app()->call($decorator, $params);
$callable = Container::getInstance()->call($decorator, $params);
}

return $callable;
Expand All @@ -127,7 +130,7 @@ public function callOnlyWith($callable, $decorators, $params)
{
$callable = $this->decorateWith($callable, $decorators);

return \App::make($callable, $params);
return Container::getInstance()->make($callable, $params);
}

/**
Expand All @@ -142,10 +145,10 @@ public function callOnlyWith($callable, $decorators, $params)
public function getCallParams($callable, array $params): array
{
if (is_callable($callable)) {
$argName = get_func_argNames($callable);
$argName = $this->getFunctionArgNames($callable);
} else {
$class = explode('@', $callable);
$argName = get_method_argNames($class[0], $class[1]);
$argName = $this->getMethodArgNames($class[0], $class[1]);
}
$parameters = array_map(function ($MArgName, $Parameters) use ($argName) {
return [$MArgName ?? $argName[count($argName) - 1] => $Parameters];
Expand All @@ -157,4 +160,34 @@ public function getCallParams($callable, array $params): array

return $parameters;
}

/**
* @throws \ReflectionException
* @throws \ReflectionException
*/
private function getMethodArgNames($className, $methodName)
{
return $this->getParameterNames(new ReflectionMethod($className, $methodName));
}

/**
* @throws \ReflectionException
*/
private function getFunctionArgNames($funcName)
{
return $this->getParameterNames(new ReflectionFunction($funcName));
}

private static function contains($haystack, $needle)
{
return mb_strpos($haystack, $needle) !== false;

}

private function getParameterNames($reflection): array
{
return array_map(function ($param) {
return $param->name;
}, $reflection->getParameters());
}
}
38 changes: 0 additions & 38 deletions src/helpers.php

This file was deleted.

5 changes: 3 additions & 2 deletions tests/CacheResultDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

use Illuminate\Container\Container;
use Illuminate\Support\Facades\App;
use Imanghafoori\Decorator\Decorators\DecoratorFactory;

class CacheResultDecoratorTest extends TestCase
{
public function testCacheResultDecorator()
{
app()->singleton('abc', abc::class);
Container::getInstance()->singleton('abc', abc::class);
\MyFacade::forgetDecorations('getGiven');
\MyFacade::decorateMethod('getGiven', DecoratorFactory::cache('hello', 2));

Expand All @@ -22,7 +23,7 @@ public function testCacheResultDecorator()

public function testPermanentCacheResultDecorator()
{
app()->singleton('abc', abc::class);
Container::getInstance()->singleton('abc', abc::class);
\MyFacade::forgetDecorations('getGiven');
\MyFacade::decorateMethod('getGiven', DecoratorFactory::foreverCache(function ($a) {
return 'cache_key_'.$a;
Expand Down
13 changes: 7 additions & 6 deletions tests/DecoratableFacadeTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

use Illuminate\Container\Container;
use Imanghafoori\Decorator\Decorator;
use Imanghafoori\Decorator\IamDecoratable;

class DecoratableFacadeTest extends TestCase
{
public function testDecoratableFacade()
{
app()->singleton('abc', abc::class);
Container::getInstance()->singleton('abc', abc::class);
\MyFacade::forgetDecorations('getGiven');
\MyFacade::decorateMethod('getGiven', function ($f) {
return function () {
Expand All @@ -29,8 +30,8 @@ public function testDecoratableFacade()

public function testDecoratableFacade2()
{
app()->singleton('abc', abc::class);
app(Decorator::class)->define('stringifyResult', [ResultCasterDecorator::class, 'toStringStaticDecorator']);
Container::getInstance()->singleton('abc', abc::class);
Container::getInstance()->make(Decorator::class)->define('stringifyResult', [ResultCasterDecorator::class, 'toStringStaticDecorator']);

\MyFacade::forgetDecorations();
\MyFacade::decorateMethod('getGiven', 'stringifyResult');
Expand All @@ -42,7 +43,7 @@ public function testDecoratableFacade2()

public function testStaticMethodsAsDecoratorsOnFacades()
{
app()->singleton('abc', abc::class);
Container::getInstance()->singleton('abc', abc::class);
\MyFacade::decorateMethod('getGiven', ResultCasterDecorator::class.'@toStringDecorator');

$this->assertIsString(\MyFacade::getGiven(1));
Expand All @@ -52,7 +53,7 @@ public function testStaticMethodsAsDecoratorsOnFacades()

public function testFacadeClassDecorators()
{
app()->singleton('abc', abc::class);
Container::getInstance()->singleton('abc', abc::class);
\MyFacade::decorateAll(ResultCasterDecorator::class.'@minimumParamZero');
\MyFacade::decorateAll(ResultCasterDecorator::class.'@toStringDecorator');

Expand All @@ -65,7 +66,7 @@ public function testFacadeClassDecorators()

public function testFacadeClassDecoratorsCombination()
{
app()->singleton('abc', abc::class);
Container::getInstance()->singleton('abc', abc::class);
\MyFacade::decorateMethod('getGiven', ResultCasterDecorator::class.'@minimumParamZero');
\MyFacade::decorateAll(ResultCasterDecorator::class.'@toStringDecorator');

Expand Down
32 changes: 13 additions & 19 deletions tests/DecoratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Container\Container;
use Imanghafoori\Decorator\Decorator;

class DecoratorTest extends TestCase
Expand Down Expand Up @@ -78,7 +79,7 @@ public function testSimpleDecoratorOnInterface()
$decorator->define('stringifyResult', $this->getResultCasterDecorator());

$decorator->decorate(ICalculator::class.'@add', 'stringifyResult');
app()->bind(ICalculator::class, Calculator::class);
Container::getInstance()->bind(ICalculator::class, Calculator::class);

$result = $decorator->call(ICalculator::class.'@add', [10, 10]);
$this->assertIsString($result);
Expand All @@ -91,13 +92,13 @@ public function testTwoDecorators()

$stringifyDecorator = function ($decorated) {
return function (...$params) use ($decorated) {
return (string) app()->call($decorated, app('decorator')->getCallParams($decorated, $params[0]));
return (string) Container::getInstance()->call($decorated, Container::getInstance()->make('decorator')->getCallParams($decorated, $params[0]));
};
};

$intifyParamsDecorator = function ($decorated) {
return function ($x, $y) use ($decorated) {
return app()->call($decorated, app('decorator')->getCallParams($decorated, [(int) $x, (int) $y]));
return Container::getInstance()->call($decorated, Container::getInstance()->make('decorator')->getCallParams($decorated, [(int) $x, (int) $y]));
};
};

Expand All @@ -122,7 +123,7 @@ public function testMultipleDecorators()
$x = ($x < -20) ? -20 : $x;
$y = ($y < -20) ? -20 : $y;

return app()->call($callback, app('decorator')->getCallParams($callback, [$x, $y]));
return Container::getInstance()->call($callback, Container::getInstance()->make('decorator')->getCallParams($callback, [$x, $y]));
};
});

Expand Down Expand Up @@ -159,7 +160,7 @@ public function getResultCasterDecorator()
{
return function ($callable) {
return function (...$params) use ($callable) {
return (string) app()->call($callable, app('decorator')->getCallParams($callable, $params[0]));
return (string) Container::getInstance()->call($callable, Container::getInstance()->make('decorator')->getCallParams($callable, $params[0]));
};
};
}
Expand All @@ -168,11 +169,11 @@ public function getResultCasterDecorator()
* @param int $max
* @return \Closure
*/
private function maxResult(int $max): \Closure
private function maxResult(int $max): Closure
{
return function ($callable) use ($max) {
return function (...$params) use ($callable, $max) {
$result = app()->call($callable, app('decorator')->getCallParams($callable, $params[0]));
$result = Container::getInstance()->call($callable, Container::getInstance()->make('decorator')->getCallParams($callable, $params[0]));

return ($result > $max) ? $max : $result;
};
Expand All @@ -192,13 +193,6 @@ public function add(int $x, int $y): int
return $x + $y;
}

public function arraySum(...$x)
{
app()->call($this->addToStr($x, $y));

return array_sum($x);
}

public function addToStr(int $x, int $y): string
{
return (string) ($x + $y);
Expand All @@ -210,7 +204,7 @@ class ResultCasterDecorator
public function toStringDecorator($callable)
{
return function (...$params) use ($callable) {
return (string) app()->call($callable, app('decorator')->getCallParams($callable, is_array($params[0]) ? $params[0] : $params));
return (string) Container::getInstance()->call($callable, Container::getInstance()->make('decorator')->getCallParams($callable, is_array($params[0]) ? $params[0] : $params));
};
}

Expand All @@ -223,28 +217,28 @@ public function minimumParamZero($callable)
}
}

return app()->call($callable, $params);
return Container::getInstance()->call($callable, $params);
};
}

public static function toStringStaticDecorator($callable)
{
return function (...$params) use ($callable) {
return (string) app()->call($callable, $params);
return (string) Container::getInstance()->call($callable, $params);
};
}

public static function toInt($callable)
{
return function (...$params) use ($callable) {
return (int) app()->call($callable, app('decorator')->getCallParams($callable, is_array($params[0]) ? $params[0] : $params));
return (int) Container::getInstance()->call($callable, Container::getInstance()->make('decorator')->getCallParams($callable, is_array($params[0]) ? $params[0] : $params));
};
}

public function _toString($callable)
{
return function (...$params) use ($callable) {
return (string) app()->call($callable, ['x' => $params[0][0], 'y' => $params[0][1]]);
return (string) Container::getInstance()->call($callable, ['x' => $params[0][0], 'y' => $params[0][1]]);
};
}
}

0 comments on commit 3a054c5

Please sign in to comment.