Skip to content

Commit

Permalink
cs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyao committed Sep 3, 2024
1 parent 31aae18 commit 0161058
Show file tree
Hide file tree
Showing 128 changed files with 1,144 additions and 1,626 deletions.
14 changes: 12 additions & 2 deletions src/aop/publish/aop.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@
* @license https://github.com/next-laboratory/next/blob/master/LICENSE
*/

use Next\Aop\Collector\AspectCollector;
use Next\Aop\Collector\PropertyAttributeCollector;

/**
* This file is part of nextphp.
*
* @see https://github.com/next-laboratory
* @license https://github.com/next-laboratory/next/blob/master/LICENSE
*/

return [
'scanDirs' => [
__DIR__ . '/app',
],
'collectors' => [
\Next\Aop\Collector\AspectCollector::class,
\Next\Aop\Collector\PropertyAttributeCollector::class,
AspectCollector::class,
PropertyAttributeCollector::class,
],
'runtimeDir' => __DIR__ . '/runtime/aop',
];
40 changes: 18 additions & 22 deletions src/aop/src/Aop.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

/**
* This file is part of MarxPHP.
* This file is part of nextphp.
*
* @link https://github.com/next-laboratory
* @license https://github.com/next-laboratory/next/blob/master/LICENSE
Expand Down Expand Up @@ -31,15 +31,14 @@ final class Aop
private Filesystem $filesystem;

private function __construct(
private array $scanDirs = [],
private array $collectors = [],
private array $scanDirs = [],
private array $collectors = [],
private string $runtimeDir = '',
)
{
) {
$this->filesystem = new Filesystem();
$this->astManager = new AstManager();
$this->runtimeDir = rtrim($runtimeDir, '/\\') . '/';
if (!$this->filesystem->isDirectory($this->runtimeDir)) {
if (! $this->filesystem->isDirectory($this->runtimeDir)) {
$this->filesystem->makeDirectory($this->runtimeDir, 0755, true);
}
$this->findClasses($this->scanDirs);
Expand All @@ -51,37 +50,34 @@ private function __construct(
} else {
$this->filesystem->exists($this->proxyMapFile) && $this->filesystem->delete($this->proxyMapFile);
}
if (!$this->filesystem->exists($this->proxyMapFile)) {
if (! $this->filesystem->exists($this->proxyMapFile)) {
touch($lockFile);
$this->filesystem->exists($this->proxyMapFile) && $this->filesystem->delete($this->proxyMapFile);
global $argv;
$cmd = PHP_BINARY . ' ' . implode(' ', $argv) . ' 2>&1';
$result = shell_exec($cmd);
if ($result) {
print($result . PHP_EOL);
echo $result . PHP_EOL;
}
// if (($pid = pcntl_fork()) == -1) {
// throw new \RuntimeException('Process fork failed.');
// }
// pcntl_wait($pid);
// if (($pid = pcntl_fork()) == -1) {
// throw new \RuntimeException('Process fork failed.');
// }
// pcntl_wait($pid);
}
Composer::getClassLoader()->addClassMap($this->getProxyMap());
$this->collect();
unset($this->filesystem, $this->astManager);
Reflection::clear();
}

private function __clone(): void
{
}
private function __clone(): void {}

public static function init(
array $scanDirs = [],
array $collectors = [],
array $scanDirs = [],
array $collectors = [],
string $runtimeDir = '',
bool $cache = false
): void
{
bool $cache = false
): void {
new self($scanDirs, $collectors, $runtimeDir, $cache);
}

Expand All @@ -106,7 +102,7 @@ private function findClasses(array $dirs): void
*/
private function getProxyMap(): array
{
if (!$this->filesystem->exists($this->proxyMapFile)) {
if (! $this->filesystem->exists($this->proxyMapFile)) {
$proxyDir = $this->runtimeDir . 'proxy/';
$this->filesystem->exists($proxyDir) || $this->filesystem->makeDirectory($proxyDir, 0755, true, true);
$this->filesystem->cleanDirectory($proxyDir);
Expand Down Expand Up @@ -150,7 +146,7 @@ private function collect(): void
foreach ($reflectionClass->getAttributes() as $attribute) {
try {
$attributeInstance = $attribute->newInstance();
if (!$attributeInstance instanceof \Attribute) {
if (! $attributeInstance instanceof \Attribute) {
foreach ($this->collectors as $collector) {
$collector::collectClass($class, $attributeInstance);
}
Expand Down
1 change: 0 additions & 1 deletion src/aop/src/AstManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;

class AstManager
{
Expand Down
9 changes: 3 additions & 6 deletions src/aop/src/Attribute/AspectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

namespace Next\Aop\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AspectConfig
{
/**
Expand All @@ -23,8 +21,7 @@ class AspectConfig
*/
public function __construct(
public string $class,
public string|array $methods = '*',
public array|string $methods = '*',
public array $params = []
) {
}
) {}
}
16 changes: 4 additions & 12 deletions src/aop/src/Collector/AbstractCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@

abstract class AbstractCollector implements CollectorInterface
{
public static function collectClass(string $class, object $attribute): void
{
}
public static function collectClass(string $class, object $attribute): void {}

public static function collectMethod(string $class, string $method, object $attribute): void
{
}
public static function collectMethod(string $class, string $method, object $attribute): void {}

public static function collectProperty(string $class, string $property, object $attribute): void
{
}
public static function collectProperty(string $class, string $property, object $attribute): void {}

public static function collectorMethodParameter(string $class, string $method, string $parameter, object $attribute)
{
}
public static function collectorMethodParameter(string $class, string $method, string $parameter, object $attribute) {}
}
11 changes: 5 additions & 6 deletions src/aop/src/Collector/AspectCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

namespace Next\Aop\Collector;

use Next\Aop\Aop;
use Next\Aop\Attribute\AspectConfig;
use Next\Aop\Contract\AspectInterface;
use Next\Aop\Aop;
use Next\Di\Reflection;
use ReflectionException;

class AspectCollector extends AbstractCollector
{
Expand All @@ -32,13 +31,13 @@ public static function collectMethod(string $class, string $method, object $attr
}

/**
* @throws ReflectionException
* @throws \ReflectionException
*/
public static function collectClass(string $class, object $attribute): void
{
if ($attribute instanceof AspectInterface) {
foreach (Reflection::class($class)->getMethods() as $reflectionMethod) {
if (!$reflectionMethod->isConstructor()) {
if (! $reflectionMethod->isConstructor()) {
self::$container[$class][$reflectionMethod->getName()][] = $attribute;
}
}
Expand All @@ -48,12 +47,12 @@ public static function collectClass(string $class, object $attribute): void
$methods = $attribute->methods;
if ($methods === '*') {
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
if (!$reflectionMethod->isConstructor()) {
if (! $reflectionMethod->isConstructor()) {
self::$container[$attribute->class][$reflectionMethod->getName()][] = $annotation;
}
}
} else {
foreach ((array)$methods as $method) {
foreach ((array) $methods as $method) {
self::$container[$attribute->class][$method][] = $annotation;
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/aop/src/Exception/PropertyHandleException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@

namespace Next\Aop\Exception;

use RuntimeException;

class PropertyHandleException extends RuntimeException
{
}
class PropertyHandleException extends \RuntimeException {}
3 changes: 1 addition & 2 deletions src/aop/src/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ class Metadata
public function __construct(
public string $className,
public bool $hasConstructor = false
) {
}
) {}
}
18 changes: 7 additions & 11 deletions src/aop/src/ProceedingJoinPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,23 @@

namespace Next\Aop;

use ArrayObject;
use Closure;
use Next\Aop\Contract\AspectInterface;

class ProceedingJoinPoint
{
/**
* @param string $class 切入的类名
* @param string $method 切入的方法
* @param ArrayObject $parameters 当前方法传递的参数列表【索引数组】
* @param \ArrayObject $parameters 当前方法传递的参数列表【索引数组】
* @param array<int, AspectInterface> $aspects
*/
public function __construct(
protected array $aspects,
protected Closure $callback,
public string $class,
public string $method,
public ArrayObject $parameters,
)
{
}
protected array $aspects,
protected \Closure $callback,
public string $class,
public string $method,
public \ArrayObject $parameters,
) {}

public function process()
{
Expand Down
16 changes: 5 additions & 11 deletions src/aop/src/PropertyHandlerVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
Expand All @@ -30,17 +29,12 @@
use PhpParser\Node\Stmt\TraitUse;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use ReflectionException;
use ReflectionNamedType;
use ReflectionUnionType;

class PropertyHandlerVisitor extends NodeVisitorAbstract
{
public function __construct(
protected Metadata $metadata
)
{
}
) {}

public function enterNode(Node $node)
{
Expand All @@ -50,7 +44,7 @@ public function enterNode(Node $node)
}

/**
* @throws ReflectionException
* @throws \ReflectionException
*/
public function leaveNode(Node $node)
{
Expand Down Expand Up @@ -79,11 +73,11 @@ public function leaveNode(Node $node)
foreach ($reflectionConstructor->getParameters() as $key => $reflectionParameter) {
$type = $reflectionParameter->getType();
if (is_null($type)
|| ($type instanceof ReflectionNamedType && ($type->isBuiltin() || $type->getName() === 'Closure'))
|| ($type instanceof \ReflectionNamedType && ($type->isBuiltin() || $type->getName() === 'Closure'))
) {
continue;
}
if ($type instanceof ReflectionUnionType) {
if ($type instanceof \ReflectionUnionType) {
$unionType = [];
foreach ($type->getTypes() as $reflectionNamedType) {
$unionType[] = ($reflectionNamedType->isBuiltin() ? '' : '\\') . $reflectionNamedType->getName();
Expand All @@ -96,7 +90,7 @@ public function leaveNode(Node $node)
}
}
$c = [];
if (!$this->metadata->hasConstructor) {
if (! $this->metadata->hasConstructor) {
$constructor = new ClassMethod('__construct', [
'params' => $params,
]);
Expand Down
23 changes: 10 additions & 13 deletions src/aop/src/ProxyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,31 @@

namespace Next\Aop;

use ArrayObject;
use Closure;
use Next\Aop\Collector\AspectCollector;
use Next\Di\Reflection;
use ReflectionException;

trait ProxyHandler
{
/**
* @throws ReflectionException
* @throws \ReflectionException
*/
protected static function __callViaProxy(string $method, Closure $callback, array $parameters): mixed
protected static function __callViaProxy(string $method, \Closure $callback, array $parameters): mixed
{
$class = static::class;
// /** @var AspectInterface $aspect */
// $pipeline = array_reduce(
// array_reverse(AspectCollector::getMethodAspects($class, $method)),
// fn($stack, $aspect) => fn(JoinPoint $joinPoint) => $aspect->process($joinPoint, $stack),
// fn(JoinPoint $joinPoint) => $joinPoint->process()
// );
$args = new ArrayObject();
// /** @var AspectInterface $aspect */
// $pipeline = array_reduce(
// array_reverse(AspectCollector::getMethodAspects($class, $method)),
// fn($stack, $aspect) => fn(JoinPoint $joinPoint) => $aspect->process($joinPoint, $stack),
// fn(JoinPoint $joinPoint) => $joinPoint->process()
// );
$args = new \ArrayObject();
$methodParameters = Reflection::methodParameterNames($class, $method);
foreach ($parameters as $key => $parameter) {
$args->offsetSet($methodParameters[$key], $parameter);
}

$aspects = AspectCollector::getMethodAspects($class, $method);
return (new ProceedingJoinPoint($aspects, $callback, $class, $method, $args))->process();
// return $pipeline(new JoinPoint($class, $method, $funcArgs, $callback));
// return $pipeline(new JoinPoint($class, $method, $funcArgs, $callback));
}
}
4 changes: 1 addition & 3 deletions src/aop/src/ProxyHandlerVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ class ProxyHandlerVisitor extends NodeVisitorAbstract
{
public function __construct(
protected Metadata $metadata
)
{
}
) {}

public function leaveNode(Node $node)
{
Expand Down
Loading

0 comments on commit 0161058

Please sign in to comment.