Skip to content

Commit

Permalink
Move some namespaces on reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Dec 11, 2023
1 parent 4874194 commit 65797db
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 72 deletions.
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@

<PossiblyFalseOperand>
<errorLevel type="suppress">
<file name="src/Component/Reflection/ClassInfoTrait.php" />
<file name="src/Component/src/Reflection/ClassInfoTrait.php" />
</errorLevel>
</PossiblyFalseOperand>

Expand Down Expand Up @@ -192,6 +192,7 @@
<directory name="src/Component/Factory" />
<directory name="src/Component/Metadata" />
<directory name="src/Component/Generator" />
<directory name="src/Component/Reflection" />
<directory name="src/Component/Storage" />
<file name="src/Bundle/Event/ResourceControllerEvent.php" />
</errorLevel>
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/DependencyInjection/SyliusResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Sylius\Component\Resource\Factory\FactoryInterface as LegacyFactoryInterface;
use Sylius\Component\Resource\Reflection\ClassReflection;
use Sylius\Resource\Factory\Factory;
use Sylius\Resource\Factory\FactoryInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Metadata;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Reflection\ClassReflection;
use Sylius\Resource\State\ProcessorInterface;
use Sylius\Resource\State\ProviderInterface;
use Sylius\Resource\State\ResponderInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Routing/CrudRoutesAttributesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace Sylius\Bundle\ResourceBundle\Routing;

use Sylius\Component\Resource\Annotation\SyliusCrudRoutes as LegacySyliusCrudRoutes;
use Sylius\Component\Resource\Reflection\ClassReflection;
use Sylius\Resource\Annotation\SyliusCrudRoutes;
use Sylius\Resource\Reflection\ClassReflection;
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Yaml\Yaml;
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Routing/RouteAttributesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace Sylius\Bundle\ResourceBundle\Routing;

use Sylius\Component\Resource\Annotation\SyliusRoute as LegacySyliusRoute;
use Sylius\Component\Resource\Reflection\ClassReflection;
use Sylius\Resource\Annotation\SyliusRoute;
use Sylius\Resource\Reflection\ClassReflection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Webmozart\Assert\Assert;
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Routing/RoutesAttributesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sylius\Bundle\ResourceBundle\Routing;

use Sylius\Component\Resource\Reflection\ClassReflection;
use Sylius\Resource\Reflection\ClassReflection;
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
use Symfony\Component\Routing\RouteCollection;

Expand Down
55 changes: 4 additions & 51 deletions src/Component/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,11 @@

namespace Sylius\Component\Resource\Reflection;

use Symfony\Component\Finder\Finder;
class_exists(\Sylius\Resource\Reflection\ClassReflection::class);

final class ClassReflection
{
public static function getResourcesByPaths(array $paths): iterable
if (false) {
final class ClassReflection extends \Sylius\Resource\Reflection\ClassReflection
{
foreach ($paths as $resourceDirectory) {
$resources = self::getResourcesByPath($resourceDirectory);

foreach ($resources as $className) {
yield $className;
}
}
}

public static function getResourcesByPath(string $path): iterable
{
$finder = new Finder();
$finder->files()->in($path)->name('*.php')->sortByName(true);

foreach ($finder as $file) {
$fileContent = (string) file_get_contents((string) $file->getRealPath());

preg_match('/namespace (.+);/', $fileContent, $matches);

$namespace = $matches[1] ?? null;

if (!preg_match('/class +([^{ ]+)/', $fileContent, $matches)) {
// no class found
continue;
}

$className = trim($matches[1]);

if (null !== $namespace) {
yield $namespace . '\\' . $className;
} else {
yield $className;
}
}
}

/**
* @psalm-param class-string $className
*
* @return \ReflectionAttribute[]
*/
public static function getClassAttributes(string $className, ?string $attributeName = null): array
{
$reflectionClass = new \ReflectionClass($className);

/** @psalm-suppress ArgumentTypeCoercion */
return $reflectionClass->getAttributes($attributeName);
}
}

2 changes: 1 addition & 1 deletion src/Component/Tests/Reflection/ClassInfoTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use App\Entity\Book;
use PHPUnit\Framework\TestCase;
use Sylius\Component\Resource\Reflection\ClassInfoTrait;
use Sylius\Resource\Reflection\ClassInfoTrait;

final class ClassInfoTraitTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Tests/Reflection/ClassReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
use App\Entity\Route\ShowBook;
use App\Entity\Route\ShowBookWithPriority;
use PHPUnit\Framework\TestCase;
use Sylius\Component\Resource\Reflection\ClassReflection;
use Sylius\Component\Resource\Tests\Dummy\DummyClassOne;
use Sylius\Component\Resource\Tests\Dummy\DummyClassTwo;
use Sylius\Component\Resource\Tests\Dummy\TraitPass;
use Sylius\Resource\Annotation\SyliusCrudRoutes;
use Sylius\Resource\Annotation\SyliusRoute;
use Sylius\Resource\Reflection\ClassReflection;

final class ClassReflectionTest extends TestCase
{
Expand Down
25 changes: 25 additions & 0 deletions src/Component/spec/Reflection/ClassReflectionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Component\Resource\Reflection;

use PhpSpec\ObjectBehavior;
use Sylius\Resource\Reflection\ClassReflection as NewClassReflection;

final class ClassReflectionSpec extends ObjectBehavior
{
function it_is_an_alias_of_the_class_reflection(): void
{
$this->shouldBeAnInstanceOf(NewClassReflection::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager as DoctrineObjectManager;
use Sylius\Component\Resource\Reflection\ClassInfoTrait;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Reflection\ClassInfoTrait;
use Sylius\Resource\State\ProcessorInterface;

final class PersistProcessor implements ProcessorInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager as DoctrineObjectManager;
use Sylius\Component\Resource\Reflection\ClassInfoTrait;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Reflection\ClassInfoTrait;
use Sylius\Resource\State\ProcessorInterface;

final class RemoveProcessor implements ProcessorInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sylius\Resource\Metadata\Resource\Factory;

use Sylius\Component\Resource\Reflection\ClassReflection;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\Metadata\MetadataInterface;
Expand All @@ -22,6 +21,7 @@
use Sylius\Resource\Metadata\RegistryInterface;
use Sylius\Resource\Metadata\Resource\ResourceMetadataCollection;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Reflection\ClassReflection;
use Sylius\Resource\Symfony\Request\State\Responder;
use Sylius\Resource\Symfony\Routing\Factory\OperationRouteNameFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Sylius\Component\Resource\Reflection;
namespace Sylius\Resource\Reflection;

final class CallableReflection
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Sylius\Component\Resource\Reflection;
namespace Sylius\Resource\Reflection;

/**
* Retrieves information about a class.
Expand Down
72 changes: 72 additions & 0 deletions src/Component/src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Reflection;

use Symfony\Component\Finder\Finder;

final class ClassReflection
{
public static function getResourcesByPaths(array $paths): iterable
{
foreach ($paths as $resourceDirectory) {
$resources = self::getResourcesByPath($resourceDirectory);

foreach ($resources as $className) {
yield $className;
}
}
}

public static function getResourcesByPath(string $path): iterable
{
$finder = new Finder();
$finder->files()->in($path)->name('*.php')->sortByName(true);

foreach ($finder as $file) {
$fileContent = (string) file_get_contents((string) $file->getRealPath());

preg_match('/namespace (.+);/', $fileContent, $matches);

$namespace = $matches[1] ?? null;

if (!preg_match('/class +([^{ ]+)/', $fileContent, $matches)) {
// no class found
continue;
}

$className = trim($matches[1]);

if (null !== $namespace) {
yield $namespace . '\\' . $className;
} else {
yield $className;
}
}
}

/**
* @psalm-param class-string $className
*
* @return \ReflectionAttribute[]
*/
public static function getClassAttributes(string $className, ?string $attributeName = null): array
{
$reflectionClass = new \ReflectionClass($className);

/** @psalm-suppress ArgumentTypeCoercion */
return $reflectionClass->getAttributes($attributeName);
}
}

class_alias(ClassReflection::class, \Sylius\Component\Resource\Reflection\ClassReflection::class);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Sylius\Component\Resource\Reflection\Filter;
namespace Sylius\Resource\Reflection\Filter;

final class FunctionArgumentsFilter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sylius\Resource\Symfony\Request;

use Sylius\Component\Resource\Reflection\Filter\FunctionArgumentsFilter;
use Sylius\Resource\Reflection\Filter\FunctionArgumentsFilter;
use Symfony\Component\HttpFoundation\Request;

final class RepositoryArgumentResolver
Expand Down
2 changes: 1 addition & 1 deletion src/Component/src/Symfony/Request/State/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

use Pagerfanta\Pagerfanta;
use Psr\Container\ContainerInterface;
use Sylius\Component\Resource\Reflection\CallableReflection;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Context\Option\RequestOption;
use Sylius\Resource\Metadata\BulkOperationInterface;
use Sylius\Resource\Metadata\CollectionOperationInterface;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Reflection\CallableReflection;
use Sylius\Resource\State\ProviderInterface;
use Sylius\Resource\Symfony\ExpressionLanguage\ArgumentParserInterface;
use Sylius\Resource\Symfony\Request\RepositoryArgumentResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

declare(strict_types=1);

namespace spec\Sylius\Component\Resource\Reflection;
namespace spec\Sylius\Resource\Reflection;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Reflection\CallableReflection;
use Sylius\Component\Resource\Tests\Dummy\RepositoryWithCallables;
use Sylius\Resource\Reflection\CallableReflection;

class CallableReflectionSpec extends ObjectBehavior
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

declare(strict_types=1);

namespace spec\Sylius\Component\Resource\Reflection\Filter;
namespace spec\Sylius\Resource\Reflection\Filter;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Reflection\CallableReflection;
use Sylius\Component\Resource\Reflection\Filter\FunctionArgumentsFilter;
use Sylius\Component\Resource\Tests\Dummy\RepositoryWithCallables;
use Sylius\Resource\Reflection\CallableReflection;
use Sylius\Resource\Reflection\Filter\FunctionArgumentsFilter;

final class FunctionArgumentsFilterSpec extends ObjectBehavior
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace spec\Sylius\Resource\Symfony\Request;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Reflection\CallableReflection;
use Sylius\Component\Resource\Tests\Dummy\RepositoryWithCallables;
use Sylius\Resource\Reflection\CallableReflection;
use Sylius\Resource\Symfony\Request\RepositoryArgumentResolver;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand Down

0 comments on commit 65797db

Please sign in to comment.