Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed May 6, 2024
1 parent c724d89 commit 9874a6f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
4 changes: 1 addition & 3 deletions src/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ private static function contains($haystack, $needle)

private function getParameterNames($reflection): array
{
return array_map(function ($param) {
return $param->name;
}, $reflection->getParameters());
return array_map(fn ($param) => $param->name, $reflection->getParameters());
}
}
7 changes: 0 additions & 7 deletions src/DecoratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@

class DecoratorServiceProvider extends ServiceProvider
{
protected $defer = true;

public function register()
{
$this->app->singleton(DecoratorFactory::class);
$this->app->singleton(Decorator::class);
$this->app->singleton('decorator', Decorator::class);
}

public function provides()
{
return [Decorator::class, 'decorator', DecoratorFactory::class];
}
}
15 changes: 7 additions & 8 deletions src/Decorators/DecoratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Imanghafoori\Decorator\Decorators;

use Closure;
use Illuminate\Container\Container;

class DecoratorFactory
{
public static function cache($key, $minutes = 1)
Expand All @@ -20,7 +23,7 @@ public static function variadicParam()
return function (...$param) use ($callable) {
$param = is_array($param[0]) ? $param[0] : $param;

return app()->call($callable, $param);
return Container::getInstance()->call($callable, $param);
};
};
}
Expand All @@ -31,21 +34,17 @@ public static function variadicParam()
* @param $remember
* @return \Closure
*/
private static function getDecoratorFactory($key, $remember, $minutes = null): \Closure
private static function getDecoratorFactory($key, $remember, $minutes = null): Closure
{
return function ($callable) use ($key, $minutes, $remember) {
return function (...$params) use ($callable, $key, $minutes, $remember) {
$cb = function () use ($callable, $params) {
return \App::call($callable, $params);
};
$cb = fn () => Container::getInstance()->call($callable, $params);

if (is_callable($key)) {
$key = $key(...$params);
}

return cache()->$remember(...array_filter([$key, $minutes, $cb], function ($el) {
return ! is_null($el);
}));
return Container::getInstance()->make('cache')->$remember(...array_filter([$key, $minutes, $cb], fn ($el) => ! is_null($el)));
};
};
}
Expand Down
4 changes: 3 additions & 1 deletion src/IamDecoratable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Imanghafoori\Decorator;

use Illuminate\Container\Container;

trait IamDecoratable
{
/**
Expand Down Expand Up @@ -63,7 +65,7 @@ public static function __callStatic($method, $args)
};

$decorators = self::getDecorations($method) + static::$classDecorations;
$callback = app(Decorator::class)->decorateWith($callback, $decorators);
$callback = Container::getInstance()->make(Decorator::class)->decorateWith($callback, $decorators);

return $callback(...$args);
}
Expand Down

0 comments on commit 9874a6f

Please sign in to comment.