-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add path resolver to init command, to add konw project paths on generate
- Loading branch information
1 parent
f002a2f
commit e56cb92
Showing
8 changed files
with
135 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\FileSystem; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
/** | ||
* @see \Rector\Core\Tests\FileSystem\InitFilePathsResolver\InitFilePathsResolverTest | ||
*/ | ||
final class InitFilePathsResolver | ||
{ | ||
/** | ||
* @return string[] | ||
*/ | ||
public function resolve(string $projectDirectory): array | ||
{ | ||
$rootDirectoryFinder = Finder::create() | ||
->directories() | ||
->depth(0) | ||
// system files | ||
->notPath('#(vendor|var|stubs|temp|templates|tmp|e2e|bin|build)#') | ||
->in($projectDirectory) | ||
->sortByName(); | ||
|
||
/** @var SplFileInfo[] $rootDirectoryFileInfos */ | ||
$rootDirectoryFileInfos = iterator_to_array($rootDirectoryFinder); | ||
|
||
$projectDirectories = []; | ||
|
||
foreach ($rootDirectoryFileInfos as $rootDirectoryFileInfo) { | ||
if (! $this->hasDirectoryFileInfoPhpFiles($rootDirectoryFileInfo)) { | ||
continue; | ||
} | ||
|
||
$projectDirectories[] = $rootDirectoryFileInfo->getRelativePathname(); | ||
} | ||
|
||
return $projectDirectories; | ||
} | ||
|
||
private function hasDirectoryFileInfoPhpFiles(SplFileInfo $rootDirectoryFileInfo): bool | ||
{ | ||
// is directory with PHP files? | ||
$phpFilesFinder = Finder::create() | ||
->files() | ||
->in($rootDirectoryFileInfo->getPathname()) | ||
->name('*.php'); | ||
|
||
return count($phpFilesFinder) !== 0; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
tests/FileSystem/InitFilePathsResolver/Fixture/first-project/config/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
some: value |
3 changes: 3 additions & 0 deletions
3
tests/FileSystem/InitFilePathsResolver/Fixture/first-project/src/SomeFile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
echo 'hi'; |
3 changes: 3 additions & 0 deletions
3
tests/FileSystem/InitFilePathsResolver/Fixture/first-project/tests/SomeFile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
echo 'hi'; |
3 changes: 3 additions & 0 deletions
3
tests/FileSystem/InitFilePathsResolver/Fixture/first-project/vendor/Trap.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
echo 'this is a trap'; |
25 changes: 25 additions & 0 deletions
25
tests/FileSystem/InitFilePathsResolver/InitFilePathsResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Tests\FileSystem\InitFilePathsResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Core\FileSystem\InitFilePathsResolver; | ||
|
||
final class InitFilePathsResolverTest extends TestCase | ||
{ | ||
private InitFilePathsResolver $initFilePathsResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->initFilePathsResolver = new InitFilePathsResolver(); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$phpDirectoryPaths = $this->initFilePathsResolver->resolve(__DIR__ . '/Fixture/first-project'); | ||
|
||
$this->assertSame(['src', 'tests'], $phpDirectoryPaths); | ||
} | ||
} |