diff --git a/composer.json b/composer.json index 98de96d..1d52979 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,10 @@ } ], "require": { - "php": "^7.1|^8.0", + "php": "^7.3|^8.0", "symfony/console": "^3.0|^4.0|^5.0", "symfony/filesystem": "^3.0|^4.0|^5.0", + "symfony/finder": "^3.0|^4.0|^5.0", "czproject/git-php": "^4.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 131432a..10f7b6c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3a538676849d123f862c013d3c35aea0", + "content-hash": "bda34dfd05a5aac8c5be6478fe6fbeda", "packages": [ { "name": "czproject/git-php", @@ -341,6 +341,67 @@ ], "time": "2021-10-28T13:39:27+00:00" }, + { + "name": "symfony/finder", + "version": "v6.0.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/finder.git", + "reference": "07debda41a4d32d33e59e6ab302af1701e15f173" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/finder/zipball/07debda41a4d32d33e59e6ab302af1701e15f173", + "reference": "07debda41a4d32d33e59e6ab302af1701e15f173", + "shasum": "" + }, + "require": { + "php": ">=8.0.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Finder\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Finds files and directories via an intuitive fluent interface", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/finder/tree/v6.0.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-11-28T15:34:37+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.23.0", @@ -3145,7 +3206,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "^7.0|^8.0" + "php": "^7.1|^8.0" }, "platform-dev": [], "plugin-api-version": "2.1.0" diff --git a/src/Directory.php b/src/Directory.php index 441aa21..de0abe2 100644 --- a/src/Directory.php +++ b/src/Directory.php @@ -4,6 +4,7 @@ use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; +use Symfony\Component\Finder\Finder; class Directory { @@ -31,15 +32,9 @@ public function getPath(string $file = null): string return $this->path; } - public function find(string $file): ?string + public function find(): Finder { - $files = glob($this->getPath($file)); - - if (count($files) > 0 && $this->fs->exists($files[0])) { - return $files[0]; - } - - return null; + return (new Finder())->in($this->getPath()); } public function contains(string $file): bool diff --git a/src/Drivers/LaravelDriver.php b/src/Drivers/LaravelDriver.php index 3d77c65..c0bf204 100644 --- a/src/Drivers/LaravelDriver.php +++ b/src/Drivers/LaravelDriver.php @@ -2,11 +2,18 @@ namespace Autopilot\Drivers; +use Symfony\Component\Filesystem\Filesystem; + class LaravelDriver extends Driver { public function matches(): bool { - return $this->repository->dir()->contains('artisan'); + if ($this->repository->dir()->contains('artisan')) { + return true; + } + + // @todo maybe all drivers should attempt subdirectories + return $this->attemptMatchInSubdirectory(); } public function setUp(): Driver @@ -78,4 +85,29 @@ private function generateAppKey(): void passthru("php {$artisan} key:generate"); } + + private function attemptMatchInSubdirectory(): bool + { + $finder = $this->repository->dir()->find() + ->depth(1) + ->name('artisan'); + + if ($finder->count() === 0) { + return false; + } + + $file = array_values(iterator_to_array($finder))[0]; + $this->moveSubdirectoryToRoot($file->getPath()); + + return true; + } + + private function moveSubdirectoryToRoot(string $subdirectory) + { + $fs = new Filesystem(); + + $dir = $this->repository->dir(); + $fs->rename($subdirectory, $dir->getPath('../temp'), true); + $fs->rename($dir->getPath('../temp'), $dir->getPath('../repository'), true); + } } diff --git a/src/Drivers/PhpWebDriver.php b/src/Drivers/PhpWebDriver.php index d9305b4..446e0ce 100644 --- a/src/Drivers/PhpWebDriver.php +++ b/src/Drivers/PhpWebDriver.php @@ -6,7 +6,7 @@ class PhpWebDriver extends Driver { public function matches(): bool { - return $this->repository->dir()->find('*.php') !== null; + return $this->repository->dir()->find()->name('*.php')->count() > 0; } public function setUp(): Driver @@ -21,12 +21,12 @@ public function serve(): Driver return $this; } - private function getPrimaryFile(): string - { - if ($index = $this->repository->dir()->find('index.php')) { - return $index; - } - - return $this->repository->dir()->find('*.php'); - } + //private function getPrimaryFile(): string + //{ + // if ($index = $this->repository->dir()->find('index.php')) { + // return $index; + // } + // + // return $this->repository->dir()->find('*.php'); + //} } diff --git a/src/Repository.php b/src/Repository.php index 50eb038..c8e224c 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -3,7 +3,6 @@ namespace Autopilot; use CzProject\GitPhp\Git; -use Symfony\Component\Filesystem\Filesystem; class Repository { diff --git a/tests/Drivers/LaravelDriverTest.php b/tests/Drivers/LaravelDriverTest.php index 1a3b135..0531b36 100644 --- a/tests/Drivers/LaravelDriverTest.php +++ b/tests/Drivers/LaravelDriverTest.php @@ -17,6 +17,17 @@ public function it_matches_default_laravel_project() $this->assertTrue($driver->matches()); } + /** @test */ + public function it_matches_laravel_project_in_subdirectory() + { + $repository = new Repository("git@bitlab.bit-academy.nl:221b9169-0a37-11ec-a943-4213e7ee7fac/d2a8bcaa-0a37-11ec-a943-4213e7ee7fac/Modelling-Is-Paramount-25a726e6-4a9f49dc.git"); + $repository->clone(); + + $driver = new LaravelDriver($repository); + + $this->assertTrue($driver->matches()); + } + /** @test */ public function it_sets_up_laravel_application() {