Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added

- feature/1201438240911768 - Combine ldl-env-util and ldl-file-finder-adapter-local to build compiled .env's from CLI
- feature/1201475529448875 - Add more options to build command, fix directory exclusion bug

### Changed

- fix/1201475529448875 - Fix directory exclusion bug
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# LDL ENV file builder

Collects ENV files from different directories and merges them into one big ENV file
Combines:

- [Env util](https://github.com/ldl-php/ldl-env-util)
- [File finder local adapter](https://github.com/ldl-php/ldl-file-finder-adapter-local)

To create a powerful finder and env compiler which will find all .env files inside a directory recursively
and compile them into one single env output file which will contain all the lines of found .env files

#### Example usage

```
php bin/build env:build -d /path/to/project/with/env/files ./out.env
```
php bin/ldl-env-build env:build -d /path/to/project/with/env/files ./out.env
```

### TODO

- Add more documentation
- Add more options to CLI ldl-env-build command
3 changes: 2 additions & 1 deletion bin/build → bin/ldl-env-build
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

require __DIR__.'/../vendor/autoload.php';

use Symfony\Component\Console\Application;
use LDL\Env\Console\Command\BuildCommand;

$console = new \LDL\Env\Console\Console();
$console = new Application();
$console->add(new BuildCommand());
$console->run();
179 changes: 92 additions & 87 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/EnvFileFinderExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
echo "[ Building compiled env file ]\n";

$envFileFinder = new EnvFileFinder(EnvFileFinderOptions::fromArray([
'directories' => [__DIR__.'/Application/Admin'],
'directories' => [__DIR__.'/Application'],
'excludedDirectories' => [__DIR__.'/Application/User']
]));

Expand Down
14 changes: 11 additions & 3 deletions src/Env/Console/Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use LDL\Env\File\Finder\EnvFileFinder;
use LDL\Env\Util\Compiler\EnvCompiler;
use LDL\Env\Util\File\Parser\EnvFileParser;
use LDL\File\Exception\FileExistsException;
use LDL\File\File;
use LDL\Framework\Base\Collection\CallableCollection;
use LDL\Framework\Helper\IterableHelper;
Expand Down Expand Up @@ -53,6 +52,12 @@ public function configure() : void
InputOption::VALUE_OPTIONAL,
'Comma separated list of files to scan',
'.env'
)
->addOption(
'print',
'p',
InputOption::VALUE_NONE,
'Print each file instead of a progress bar'
);
}

Expand All @@ -74,7 +79,7 @@ private function build(
{
$start = hrtime(true);
$excludedDirectories = $input->getOption('excluded-directories');
$verbose = (bool) $input->getOption('verbose');
$verbose = (bool) $input->getOption('print');

try{
$finderOptions = EnvFileFinderOptions::fromArray([
Expand All @@ -87,6 +92,9 @@ private function build(

if(!$verbose) {
$compilerProgress = new ProgressBar($output);
$compilerProgress->setBarCharacter("▩");
$compilerProgress->setEmptyBarCharacter("▢");
$compilerProgress->setProgressCharacter("▶");
$compilerProgress->setOverwrite(true);
}

Expand Down Expand Up @@ -122,7 +130,7 @@ static function($file, $files) use ($output, $compilerProgress){

$file = File::create($input->getArgument('output-file'), (string)$result, 0644, true);

$output->writeln("\n<info>Wrote compiled env file: $file</info>\n");
$output->writeln("\n\n<info>Wrote compiled env file: $file</info>\n");

}catch(\Exception $e) {

Expand Down
37 changes: 0 additions & 37 deletions src/Env/Console/Console.php

This file was deleted.

36 changes: 7 additions & 29 deletions src/Env/File/Finder/EnvFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use LDL\File\Finder\FoundFile;
use LDL\File\Validator\FileNameValidator;
use LDL\File\Validator\FileTypeValidator;
use LDL\File\Validator\PathValidator;
use LDL\Framework\Helper\IterableHelper;
use LDL\Validators\Chain\AndValidatorChain;
use LDL\Validators\Chain\Dumper\ValidatorChainExprDumper;
use LDL\Validators\Chain\Item\Collection\ValidatorChainItemCollection;
use LDL\Validators\Chain\OrValidatorChain;

Expand Down Expand Up @@ -45,50 +47,26 @@ public function find(bool $cache = false) : ReadableFileCollection

$options = $this->options;

$validators = new ValidatorChainItemCollection([
$validators = new AndValidatorChain([
new FileTypeValidator([FileTypeConstants::FILE_TYPE_REGULAR])
]);

foreach($this->options->getFiles() as $file){
$validators->append(new FileNameValidator($file));
}

$validators = new AndValidatorChain($validators);

$excludedChain = new AndValidatorChain();

if(count($options->getExcludedDirectories()) > 0){
$excludedDirsNameChain = new OrValidatorChain();

foreach($options->getExcludedDirectories() as $dir){
$excludedDirsNameChain->getChainItems()->append(new FileNameValidator($dir, true));
$validators->getChainItems()->append(new PathValidator($dir, true));
}

$excludedChain->getChainItems()->append($excludedDirsNameChain);
}

if(count($options->getExcludedFiles()) > 0){
$excludedFilesNameChain = new OrValidatorChain();

foreach($options->getExcludedFiles() as $file){
$excludedFilesNameChain->getChainItems()->append(new FileNameValidator($file, true));
$validators->getChainItems()->append(new FileNameValidator($file, true));
}

$excludedChain->getChainItems()->append($excludedFilesNameChain);
}

if(count($excludedChain->getChainItems()) > 0) {
$validators->getChainItems()->append($excludedChain);
}

$filesNameChain = new OrValidatorChain();

foreach($options->getFiles() as $file){
$filesNameChain->getChainItems()->append(new FileNameValidator($file));
foreach($this->options->getFiles() as $file){
$validators->getChainItems()->append(new FileNameValidator($file));
}

$validators->getChainItems()->append($filesNameChain);

$finder = new LocalFileFinderAdapter($validators);
$foundFiles = iterator_to_array($finder->find($this->options->getDirectories()), false);

Expand Down