Skip to content

Commit

Permalink
Merge pull request #573 from phel-lang/fix/phel-config-not-accessible…
Browse files Browse the repository at this point in the history
…-as-array

PhelConfig not accessible as array
  • Loading branch information
Chemaclass committed Feb 16, 2023
2 parents 844ec99 + dba01bc commit c140b7d
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/php/Command/Domain/Finder/ComposerVendorDirectoriesFinder.php
Expand Up @@ -5,10 +5,12 @@
namespace Phel\Command\Domain\Finder;

use Phel\Command\CommandConfig;

use Phel\Config\PhelConfig;
use Phel\Config\PhelConfigException;
use Phel\Phel;

use function dirname;
use function is_array;

final class ComposerVendorDirectoriesFinder implements VendorDirectoriesFinderInterface
{
Expand All @@ -28,8 +30,8 @@ public function findPhelSourceDirectories(): array

foreach (glob($pattern) as $phelConfigPath) {
$pathPrefix = dirname($phelConfigPath);
/** @psalm-suppress UnresolvableInclude */
$sourceDirectories = (require $phelConfigPath)[CommandConfig::SRC_DIRS] ?? [];
$phelConfig = $this->parsePhelConfigAsArray($phelConfigPath);
$sourceDirectories = $phelConfig[CommandConfig::SRC_DIRS] ?? [];

foreach ($sourceDirectories as $directory) {
$result[] = $pathPrefix . '/' . $directory;
Expand All @@ -38,4 +40,23 @@ public function findPhelSourceDirectories(): array

return $result;
}

private function parsePhelConfigAsArray(string $phelConfigPath): array
{
/**
* @psalm-suppress UnresolvableInclude
*
* @var array|PhelConfig|mixed $phelConfig
*/
$phelConfig = require $phelConfigPath;
if ($phelConfig instanceof PhelConfig) {
return $phelConfig->jsonSerialize();
}

if (!is_array($phelConfig)) {
throw PhelConfigException::wrongType();
}

return $phelConfig;
}
}
21 changes: 21 additions & 0 deletions src/php/Config/PhelConfigException.php
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Phel\Config;

use Phel\Phel;
use RuntimeException;

final class PhelConfigException extends RuntimeException
{
public static function wrongType(): self
{
return new self(
sprintf(
'The "%s" must return an array or a PhelConfig object',
Phel::PHEL_CONFIG_FILE_NAME,
),
);
}
}
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace PhelTest\Integration\Command\Domain\DirectoryFinder;

use Phel\Command\Domain\Finder\ComposerVendorDirectoriesFinder;
use Phel\Config\PhelConfigException;
use PHPUnit\Framework\TestCase;

final class ComposerVendorDirectoriesFinderTest extends TestCase
{
public function test_exception_when_wrong_type(): void
{
$this->expectExceptionObject(PhelConfigException::wrongType());

$finder = new ComposerVendorDirectoriesFinder(vendorDirectory: __DIR__ . '/wrong-testing-vendor');
$finder->findPhelSourceDirectories();
}

public function test_find_phel_source_directories(): void
{
$finder = new ComposerVendorDirectoriesFinder(vendorDirectory: __DIR__ . '/testing-vendor');
$dirs = $finder->findPhelSourceDirectories();

self::assertCount(2, $dirs);
self::assertMatchesRegularExpression('#.*/testing-vendor/root-1/root-2/custom-src-2#', $dirs[0]);
self::assertMatchesRegularExpression('#.*/testing-vendor/root-1/root-3/custom-src-3#', $dirs[1]);
}
}
@@ -0,0 +1 @@
(ns directory-finder\testing-vendor\root-2\hi)
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Phel\Command\CommandConfig;

return [
CommandConfig::SRC_DIRS => ['custom-src-2'],
];
@@ -0,0 +1 @@
(ns directory-finder\testing-vendor\root-3\hi)
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

use Phel\Config\PhelConfig;

return (new PhelConfig())
->setSrcDirs(['custom-src-3']);
@@ -0,0 +1 @@
(ns directory-finder\wrong-testing-vendor\root-1\root-2\custom-src\hi)
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

return new stdClass();

0 comments on commit c140b7d

Please sign in to comment.