Skip to content

Commit

Permalink
Merge pull request #672 from phel-lang/feat/phel-config-main-php-path
Browse files Browse the repository at this point in the history
Add MainPhpPath property to PhelOutConfig
  • Loading branch information
Chemaclass committed Mar 16, 2024
2 parents 3fd0739 + 9550776 commit 5bd1a29
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
## Unreleased

* Require PHP>=8.2
* Add `PhelOutConfig->setMainPhpPath()`
* in favor of `->setMainPhpFilename()`

## [0.12.0](https://github.com/phel-lang/phel-lang/compare/v0.11.0...v0.12.0) - 2023-11-01

Expand Down
3 changes: 1 addition & 2 deletions phel-config.php
Expand Up @@ -8,9 +8,8 @@
->setVendorDir('vendor')
->setErrorLogFile('data/error.log')
->setOut((new \Phel\Config\PhelOutConfig())
->setDestDir('out')
->setMainPhelNamespace('phel\core')
->setMainPhpFilename('index'))
->setMainPhpPath('out/index.php'))
->setExport((new \Phel\Config\PhelExportConfig())
->setDirectories(['src/phel'])
->setNamespacePrefix('PhelGenerated')
Expand Down
94 changes: 73 additions & 21 deletions src/php/Config/PhelOutConfig.php
Expand Up @@ -6,6 +6,9 @@

use JsonSerializable;

/**
* @psalm-suppress DeprecatedProperty
*/
final class PhelOutConfig implements JsonSerializable
{
public const DEST_DIR = 'dir';
Expand All @@ -16,38 +19,54 @@ final class PhelOutConfig implements JsonSerializable

public const MAIN_PHP_PATH = 'main-php-path';

private string $destDir = 'out';
private const DEST_DIR_DEFAULT = 'out';

private const PHP_FILENAME_DEFAULT = 'index.php';

private string $mainPhelNamespace = '';

private string $mainPhpFilename = 'index';
private string $destDir = '';

/** @deprecated in favor of $mainPhpPath */
private string $mainPhpFilename = '';

private string $mainPhpPath = '';

public static function fromArray(array $array): self
{
$self = new self();
if (isset($array[self::DEST_DIR])) {
$self->destDir = $array[self::DEST_DIR];
}

if (isset($array[self::MAIN_PHEL_NAMESPACE])) {
$self->mainPhelNamespace = $array[self::MAIN_PHEL_NAMESPACE];
}

if (isset($array[self::DEST_DIR])) {
$self->destDir = $array[self::DEST_DIR];
}

if (isset($array[self::MAIN_PHP_FILENAME])) {
$self->mainPhpFilename = $array[self::MAIN_PHP_FILENAME];
}

if (isset($array[self::MAIN_PHP_PATH])) {
$self->mainPhpPath = $array[self::MAIN_PHP_PATH];
}

return $self;
}

public function getDestDir(): string
public function jsonSerialize(): array
{
return $this->destDir;
return [
self::MAIN_PHEL_NAMESPACE => $this->mainPhelNamespace,
self::DEST_DIR => $this->getDestDir(),
self::MAIN_PHP_FILENAME => $this->getPhpFilename(),
self::MAIN_PHP_PATH => $this->getMainPhpPath(),
];
}

public function setDestDir(string $destDir): self
public function setMainPhelNamespace(string $namespace): self
{
$this->destDir = $destDir;
$this->mainPhelNamespace = $namespace;
return $this;
}

Expand All @@ -56,35 +75,68 @@ public function getMainPhelNamespace(): string
return $this->mainPhelNamespace;
}

public function setMainPhelNamespace(string $namespace): self
public function setMainPhpPath(string $path): self
{
$this->mainPhelNamespace = $namespace;
$this->mainPhpPath = rtrim($path, '.php') . '.php';
return $this;
}

public function getMainPhpPath(): string
{
return sprintf('%s/%s.php', $this->destDir, $this->mainPhpFilename);
if ($this->mainPhpPath !== '') {
return $this->mainPhpPath;
}

return sprintf(
'%s/%s',
$this->getDestDir(),
$this->getPhpFilename(),
);
}

/**
* @deprecated in favor of setMainPhpPath()
*/
public function setMainPhpFilename(string $name): self
{
$this->mainPhpFilename = $name;
return $this;
}

public function setDestDir(string $destDir): self
{
$this->destDir = $destDir;
return $this;
}

public function shouldCreateEntryPointPhpFile(): bool
{
return (bool) $this->mainPhelNamespace;
return (bool)$this->mainPhelNamespace;
}

public function jsonSerialize(): array
private function getDestDir(): string
{
return [
self::DEST_DIR => $this->destDir,
self::MAIN_PHEL_NAMESPACE => $this->mainPhelNamespace,
self::MAIN_PHP_FILENAME => $this->mainPhpFilename,
self::MAIN_PHP_PATH => $this->getMainPhpPath(),
];
if ($this->destDir !== '') {
return $this->destDir;
}

if ($this->mainPhpPath !== '') {
return explode('/', $this->mainPhpPath)[0];
}

return self::DEST_DIR_DEFAULT;
}

private function getPhpFilename(): string
{
if ($this->mainPhpPath !== '') {
return explode('/', $this->mainPhpPath)[1];
}

if ($this->mainPhpFilename !== '') {
return rtrim($this->mainPhpFilename, '.php') . '.php';
}

return self::PHP_FILENAME_DEFAULT;
}
}
23 changes: 9 additions & 14 deletions tests/php/Unit/Compiler/Config/PhelConfigTest.php
Expand Up @@ -18,18 +18,13 @@ public function test_json_serialize(): void
->setTestDirs(['another/directory'])
->setVendorDir('vendor')
->setErrorLogFile('error-log.file')
->setOut(
(new PhelOutConfig())
->setDestDir('out')
->setMainPhelNamespace('test-ns/boot')
->setMainPhpFilename('custom-index'),
)
->setExport(
(new PhelExportConfig())
->setDirectories(['some/other/dir'])
->setNamespacePrefix('Generated')
->setTargetDirectory('src/Generated'),
)
->setOut((new PhelOutConfig())
->setMainPhpPath('out/custom-index.php')
->setMainPhelNamespace('test-ns/boot'), )
->setExport((new PhelExportConfig())
->setDirectories(['some/other/dir'])
->setNamespacePrefix('Generated')
->setTargetDirectory('src/Generated'))
->setIgnoreWhenBuilding(['src/ignore.me'])
->setKeepGeneratedTempFiles(true)
->setFormatDirs(['src', 'tests', 'phel']);
Expand All @@ -40,9 +35,9 @@ public function test_json_serialize(): void
'vendor-dir' => 'vendor',
'error-log-file' => 'error-log.file',
'out' => [
'dir' => 'out',
'main-phel-namespace' => 'test-ns/boot',
'main-php-filename' => 'custom-index',
'dir' => 'out',
'main-php-filename' => 'custom-index.php',
'main-php-path' => 'out/custom-index.php',
],
'export' => [
Expand Down
117 changes: 117 additions & 0 deletions tests/php/Unit/Compiler/Config/PhelOutConfigTest.php
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace PhelTest\Unit\Compiler\Config;

use Phel\Config\PhelOutConfig;
use PHPUnit\Framework\TestCase;

final class PhelOutConfigTest extends TestCase
{
public function test_default(): void
{
$config = (new PhelOutConfig());

$expected = [
PhelOutConfig::MAIN_PHEL_NAMESPACE => '',
PhelOutConfig::DEST_DIR => 'out',
PhelOutConfig::MAIN_PHP_FILENAME => 'index.php',
PhelOutConfig::MAIN_PHP_PATH => 'out/index.php',
];

self::assertSame($expected, $config->jsonSerialize());
}

public function test_defined_phel_ns(): void
{
$config = (new PhelOutConfig())
->setMainPhelNamespace('test-ns/boot');

$expected = [
PhelOutConfig::MAIN_PHEL_NAMESPACE => 'test-ns/boot',
PhelOutConfig::DEST_DIR => 'out',
PhelOutConfig::MAIN_PHP_FILENAME => 'index.php',
PhelOutConfig::MAIN_PHP_PATH => 'out/index.php',
];

self::assertSame($expected, $config->jsonSerialize());
}

public function test_dest_dir(): void
{
$config = (new PhelOutConfig())
->setDestDir('custom-out');

$expected = [
PhelOutConfig::MAIN_PHEL_NAMESPACE => '',
PhelOutConfig::DEST_DIR => 'custom-out',
PhelOutConfig::MAIN_PHP_FILENAME => 'index.php',
PhelOutConfig::MAIN_PHP_PATH => 'custom-out/index.php',
];

self::assertSame($expected, $config->jsonSerialize());
}

public function test_dest_dir_and_main_php_filename(): void
{
$config = (new PhelOutConfig())
->setDestDir('custom-out')
->setMainPhpFilename('custom-index');

$expected = [
PhelOutConfig::MAIN_PHEL_NAMESPACE => '',
PhelOutConfig::DEST_DIR => 'custom-out',
PhelOutConfig::MAIN_PHP_FILENAME => 'custom-index.php',
PhelOutConfig::MAIN_PHP_PATH => 'custom-out/custom-index.php',
];

self::assertSame($expected, $config->jsonSerialize());
}

public function test_main_php_path_over_php_filename(): void
{
$config = (new PhelOutConfig())
->setMainPhpPath('custom-out/custom-index.php')
->setMainPhpFilename('other-name');

$expected = [
PhelOutConfig::MAIN_PHEL_NAMESPACE => '',
PhelOutConfig::DEST_DIR => 'custom-out',
PhelOutConfig::MAIN_PHP_FILENAME => 'custom-index.php',
PhelOutConfig::MAIN_PHP_PATH => 'custom-out/custom-index.php',
];

self::assertSame($expected, $config->jsonSerialize());
}

public function test_main_php_path(): void
{
$config = (new PhelOutConfig())
->setMainPhpPath('custom-out/custom-index.php');

$expected = [
PhelOutConfig::MAIN_PHEL_NAMESPACE => '',
PhelOutConfig::DEST_DIR => 'custom-out',
PhelOutConfig::MAIN_PHP_FILENAME => 'custom-index.php',
PhelOutConfig::MAIN_PHP_PATH => 'custom-out/custom-index.php',
];

self::assertSame($expected, $config->jsonSerialize());
}

public function test_main_php_path_without_ext(): void
{
$config = (new PhelOutConfig())
->setMainPhpPath('custom-out/custom-index');

$expected = [
PhelOutConfig::MAIN_PHEL_NAMESPACE => '',
PhelOutConfig::DEST_DIR => 'custom-out',
PhelOutConfig::MAIN_PHP_FILENAME => 'custom-index.php',
PhelOutConfig::MAIN_PHP_PATH => 'custom-out/custom-index.php',
];

self::assertSame($expected, $config->jsonSerialize());
}
}

0 comments on commit 5bd1a29

Please sign in to comment.