Skip to content

Commit

Permalink
remove vfs, use target dir instead
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Jun 30, 2021
1 parent a5b01f3 commit 9efafb8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
11 changes: 3 additions & 8 deletions bin/fpp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace Fpp;

use Nette\PhpGenerator\PsrPrinter;
use org\bovigo\vfs\vfsStream;

$path1 = \realpath(__DIR__ . '/../../../../');
$path2 = \realpath(__DIR__ . '/../');
Expand Down Expand Up @@ -48,7 +47,7 @@
$config = [
'use_strict_types' => true,
'source' => '.',
'target' => 'composer', // composer or vfs
'target' => '*',
'success_msg' => 'Successfully generated and written to disk',
'printer' => fn () => (new PsrPrinter())->setTypeResolving(false),
'file_parser' => parseFile,
Expand Down Expand Up @@ -82,7 +81,7 @@
return [
'use_strict_types' => true,
'source' => '.',
'target' => 'composer', // composer or vfs
'target' => '*', // * = use composer settings, otherwise give path here
'success_msg' => 'Successfully generated and written to disk',
'printer' => fn () => (new PsrPrinter())->setTypeResolving(false),
'file_parser' => parseFile,
Expand Down Expand Up @@ -117,10 +116,6 @@

$config = Configuration::fromArray($config);

$locatePath = $config->target() === 'vfs'
? $config->locatePathFromVfs(vfsStream::setup('quote-exchange-vfs'))
: $config->locatePathFromComposer($autoloader);

runFpp($config, $config->source(), $locatePath);
runFpp($config, $autoloader);

echo $config->successMessage() . "\n";
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"php": "^7.4",
"phunkie/phunkie": "^0.11.1",
"nette/php-generator": "^3.4.0",
"nette/utils": "^3.1.2",
"mikey179/vfsstream": "^1.6.8"
"nette/utils": "^3.1.2"
},
"require-dev": {
"kahlan/kahlan": "^4.7.5",
Expand Down
28 changes: 13 additions & 15 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Closure;
use Composer\Autoload\ClassLoader;
use InvalidArgumentException;
use org\bovigo\vfs\vfsStreamDirectory;
use RuntimeException;

class Configuration
Expand All @@ -35,16 +34,6 @@ class Configuration
/** @param array<class-string,TypeConfiguration> $types */
public function __construct(bool $useStrictTypes, string $source, string $target, string $successMessage, callable $printer, callable $fileParser, ?string $comment, array $types)
{
if (! \in_array($target, self::availableTargets)) {
throw new InvalidArgumentException(
\sprintf(
'Target must be one of %s, %s given',
\implode(' or ', self::availableTargets),
$target
)
);
}

$this->useStrictTypes = $useStrictTypes;
$this->source = $source;
$this->target = $target;
Expand Down Expand Up @@ -180,7 +169,16 @@ public function target(): string
return $this->target;
}

public function locatePathFromComposer(ClassLoader $classLoader): Closure
public function locatePath(ClassLoader $classLoader): Closure
{
if ('*' !== $this->target) {
return $this->locatePathFromTarget();
}

return $this->locatePathFromComposer($classLoader);
}

private function locatePathFromComposer(ClassLoader $classLoader): Closure
{
$prefixesPsr4 = $classLoader->getPrefixesPsr4();
$prefixesPsr0 = $classLoader->getPrefixes();
Expand All @@ -190,10 +188,10 @@ public function locatePathFromComposer(ClassLoader $classLoader): Closure
};
}

public function locatePathFromVfs(vfsStreamDirectory $directory): Closure
private function locatePathFromTarget(): Closure
{
return function (string $classname) use ($directory) {
return $directory->url() . DIRECTORY_SEPARATOR . \strtr($classname, '\\', DIRECTORY_SEPARATOR) . '.php';
return function (string $classname): string {
return $this->target . DIRECTORY_SEPARATOR . \strtr($classname, '\\', DIRECTORY_SEPARATOR) . '.php';
};
}

Expand Down
18 changes: 9 additions & 9 deletions src/Functions/fpp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@

namespace Fpp;

use Composer\Autoload\ClassLoader;
use Fpp\Type\Data\Data;
use Fpp\Type\Enum\Enum;
use Nette\PhpGenerator\PhpFile;
use org\bovigo\vfs\vfsStreamDirectory;
use Phunkie\Types\Pair;

const runFpp = 'Fpp\runFpp';

function runFpp(Configuration $config, string $path, \Closure $locatePath)
function runFpp(Configuration $config, ClassLoader $classLoader): void
{
$locatePath = $config->locatePath($classLoader);

$parser = \array_reduce(
\array_filter(
$config->types(),
Expand All @@ -33,9 +35,7 @@ function runFpp(Configuration $config, string $path, \Closure $locatePath)

$definitions = \array_map(
fn ($f) => Pair(($config->fileParser())($parser)->run(\file_get_contents($f)), $f),
scan(
$path
)
scan($config->source())
);

$definitions = \array_map(
Expand Down Expand Up @@ -94,13 +94,13 @@ function (array $ds, array $nds) {
}
}

const registerVfsAutoloader = 'Fpp\registerVfsAutoloader';
const registerFppTargetAutoloader = 'Fpp\registerFppTargetAutoloader';

function registerVfsAutoloader(vfsStreamDirectory $vfs)
function registerFppTargetAutoloader(string $targetDirectory): void
{
\spl_autoload_register(
function (string $className) use ($vfs) {
$file = $vfs->url() . DIRECTORY_SEPARATOR . \strtr($className, '\\', DIRECTORY_SEPARATOR) . '.php';
function (string $className) use ($targetDirectory) {
$file = $targetDirectory . DIRECTORY_SEPARATOR . \strtr($className, '\\', DIRECTORY_SEPARATOR) . '.php';

if (\file_exists($file)) {
require_once $file;
Expand Down

0 comments on commit 9efafb8

Please sign in to comment.