Skip to content

Commit

Permalink
Create a MultipleDirClassSet
Browse files Browse the repository at this point in the history
This way we can specify just the directories we want it to scan,
as opposed to include all dirs in the root and then
excluding all dirs we don't want in the set.
  • Loading branch information
Herberto Graca committed Jul 27, 2023
1 parent 74ee2e2 commit a9b0366
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/MultipleDirClassSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

namespace Arkitect;

use Symfony\Component\Finder\Finder;

class MultipleDirClassSet implements ClassSetInterface
{
/** @var string[] */
private $directoryList;

/** @var array */
private $exclude;

private function __construct(string ...$directoryList)
{
$this->directoryList = $directoryList;
$this->exclude = [];
}

public static function fromDir(string ...$directoryList): self
{
return new self(...$directoryList);
}

public function excludePath(string $pattern): ClassSetInterface
{
$this->exclude[] = Glob::toRegex($pattern);

return $this;
}

public function getDir(): string
{
return implode(', ', $this->directoryList);
}

public function getIterator(): \Traversable
{
$finder = (new Finder())
->files()
->in($this->directoryList)
->name('*.php')
->sortByName()
->followLinks()
->ignoreUnreadableDirs(true)
->ignoreVCS(true);

if ($this->exclude) {
$finder->notPath($this->exclude);
}

return $finder;
}
}
128 changes: 128 additions & 0 deletions tests/Unit/MultipleDirClassSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace Arkitect\Tests\Unit;

use Arkitect\MultipleDirClassSet;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use SplFileInfo;

class MultipleDirClassSetTest extends TestCase
{
public function test_can_exclude_files_or_directories_from_multiple_dir_class_set(): void
{
$path = $this->createMvcProjectStructure();

$set = MultipleDirClassSet::fromDir($path . '/Controller', $path . '/Model')
->excludePath("Repository");

$expected = [
$path . '/Controller/CatalogController.php',
$path . '/Controller/Foo.php',
$path . '/Controller/ProductsController.php',
$path . '/Controller/UserController.php',
$path . '/Controller/YieldController.php',
$path . '/Model/Catalog.php',
$path . '/Model/Products.php',
$path . '/Model/User.php',
];
$actual = array_values(array_map(function ($item) {
/** @var SplFileInfo $item */
return $item->getPathname();
}, iterator_to_array($set)));
self::assertEquals($expected, $actual);
}

public function test_can_exclude_files_or_directories(): void
{
$path = $this->createMvcProjectStructure();

$set = MultipleDirClassSet::fromDir($path)
->excludePath('Model')
->excludePath('ContainerAwareInterface');

$expected = [
'Controller/CatalogController.php',
'Controller/Foo.php',
'Controller/ProductsController.php',
'Controller/UserController.php',
'Controller/YieldController.php',
'Services/UserService.php',
'View/CatalogView.php',
'View/ProductView.php',
'View/UserView.php',
];

$actual = array_values(array_map(function ($item) {
/** @var SplFileInfo $item */
return $item->getRelativePathname();
}, iterator_to_array($set)));

self::assertEquals($expected, $actual);
}

public function test_can_exclude_glob_patterns(): void
{
$path = $this->createMvcProjectStructure();

$set = MultipleDirClassSet::fromDir($path)
->excludePath('*Catalog*');

$expected = [
'ContainerAwareInterface.php',
'Controller/Foo.php',
'Controller/ProductsController.php',
'Controller/UserController.php',
'Controller/YieldController.php',
'Model/Products.php',
'Model/Repository/ProductsRepository.php',
'Model/Repository/UserRepository.php',
'Model/User.php',
'Services/UserService.php',
'View/ProductView.php',
'View/UserView.php',
];

$actual = array_values(array_map(function ($item) {
return $item->getRelativePathname();
}, iterator_to_array($set)));

self::assertEquals($expected, $actual);
}

protected function createMvcProjectStructure(): string
{
$structure = [
'Controller' => [
'CatalogController.php' => '',
'Foo.php' => '',
'ProductsController.php' => '',
'UserController.php' => '',
'YieldController.php' => '',
],
'Model' => [
'Repository' => [
'CatalogRepository.php' => '',
'ProductsRepository.php' => '',
'UserRepository.php' => '',
],
'Catalog.php' => '',
'Products.php' => '',
'User.php' => '',
],
'Services' => [
'UserService.php' => '',
],
'View' => [
'CatalogView.php' => '',
'ProductView.php' => '',
'UserView.php' => '',
],
'ContainerAwareInterface.php' => '',
];

return vfsStream::setup('root', null, $structure)->url();
}
}

0 comments on commit a9b0366

Please sign in to comment.