Skip to content

Commit

Permalink
feat: add directory validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuftaufiq committed May 21, 2022
1 parent 7124b31 commit 4a69dda
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 26 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"mnapoli/silly": "^1.8",
"php-di/php-di": "^6.3",
"symfony/finder": "^5.4",
"spatie/enum": "^3.12"
"spatie/enum": "^3.12",
"symfony/filesystem": "^5.4"
},
"require-dev": {
"psy/psysh": "@stable",
Expand Down
2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Application

public const APP_VERSION = '0.1.0';

public const APP_REPOSITORY = 'https://github.com/yusuftaufiq/codeigniter3-ide-helper';

protected array $commands;

protected \Silly\Application $silly;
Expand Down
18 changes: 9 additions & 9 deletions src/Commands/GenerateHelperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GenerateHelperCommand implements Command
protected array $options = [
'--dir' => 'CodeIgniter 3 root directory',
'--pattern' => 'Pattern in string or regex to match files',
'--output' => 'Output filename'
'--output' => 'Output path of generated file',
];

protected SillyApplication $app;
Expand All @@ -56,14 +56,14 @@ public function execute(): void
}

public function __invoke(
string $dir = '/./',
string $dir = './',
array $pattern = [],
string $output = '/./_ide_helper.php'
) {
$this->facade
->withDirectory($dir)
->withPatterns($pattern)
->withOutput($output)
->generate();
string $output = '_ide_helper.php'
): int {
$this->facade->withDirectory($dir);
$this->facade->withPatterns($pattern);
$this->facade->withOutputPath($output);

return $this->facade->generate();
}
}
2 changes: 2 additions & 0 deletions src/Contracts/FileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function setPatterns(array $patterns): self;

public function getFirstFile(): ?SplFileInfo;

public function isDirectoryExists(): bool;

/**
* Undocumented function
*
Expand Down
52 changes: 49 additions & 3 deletions src/Facades/GenerateHelperFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Haemanthus\CodeIgniter3IdeHelper\Facades;

use Haemanthus\CodeIgniter3IdeHelper\Application;
use Haemanthus\CodeIgniter3IdeHelper\Contracts\FileWriter;
use Silly\Command\Command as SillyCommand;
use Symfony\Component\Console\Style\SymfonyStyle;

class GenerateHelperFacade
{
Expand All @@ -12,14 +15,20 @@ class GenerateHelperFacade

protected FileWriter $writer;

protected SymfonyStyle $io;

protected int $availableAskAttempts = 2;

public function __construct(
ReaderFacade $reader,
ParserFacade $parser,
FileWriter $writer
FileWriter $writer,
SymfonyStyle $io
) {
$this->reader = $reader;
$this->parser = $parser;
$this->writer = $writer;
$this->io = $io;
}

public function withDirectory(string $directory): self
Expand All @@ -36,20 +45,57 @@ public function withPatterns(array $patterns): self
return $this;
}

public function withOutput(string $path): self
public function withOutputPath(string $path): self
{
$this->writer->setOutputPath($path);

return $this;
}

public function generate(): void
protected function checkDirectory(): bool
{
while (
$this->availableAskAttempts > 0
&& $this->reader->isAllDirectoryExists() === false
) {
$this->availableAskAttempts -= 1;

$this->io->error("CodeIgniter 3 directory can't be found");

$this->withDirectory($this->io->ask('Please enter a valid CodeIgniter 3 directory again', './'));
}

if ($this->availableAskAttempts === 0) {
$repository = Application::APP_REPOSITORY;

$message = <<<TXT
Unfortunately, we still can't find your CodeIgniter 3 directory.
Please try again or submit the issue to our repository at ${repository}.
TXT;

$this->io->error(preg_replace('/\s+/', ' ', $message));

return false;
}

return true;
}

public function generate(): int
{
if ($this->checkDirectory() === false) {
return SillyCommand::FAILURE;
}

$structuralElements = array_merge(
$this->parser->parseAutoloadFile($this->reader->getAutoloadFile()),
$this->parser->parseClassFiles($this->reader->getClassFiles()),
);

$this->writer->write($structuralElements);

$this->io->success('Successfully generated IDE Helper file');

return SillyCommand::SUCCESS;
}
}
8 changes: 8 additions & 0 deletions src/Facades/ReaderFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function setPatterns(array $patterns): self
return $this;
}

public function isAllDirectoryExists(): bool
{
return $this->autoloadReader->isDirectoryExists()
&& $this->coreReader->isDirectoryExists()
&& $this->controllerReader->isDirectoryExists()
&& $this->modelReader->isDirectoryExists();
}

public function getAutoloadFile(): ?SplFileInfo
{
return $this->autoloadReader->getFirstFile();
Expand Down
18 changes: 12 additions & 6 deletions src/Factories/FileReaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Haemanthus\CodeIgniter3IdeHelper\Enums\FileType;
use Haemanthus\CodeIgniter3IdeHelper\Readers\AutoloadFileReader;
use Haemanthus\CodeIgniter3IdeHelper\Readers\ClassFileReader;
use Symfony\Component\Filesystem\Filesystem;

class FileReaderFactory
{
Expand All @@ -17,25 +18,30 @@ class FileReaderFactory

protected FileFinderFactory $finder;

public function __construct(FileFinderFactory $finder)
{
protected Filesystem $fs;

public function __construct(
FileFinderFactory $finder,
Filesystem $fs
) {
$this->finder = $finder;
$this->fs = $fs;
}

public function create(FileType $type): FileReader
{
switch (true) {
case $type->equals(FileType::autoload()):
return new AutoloadFileReader($this->finder);
return new AutoloadFileReader($this->finder, $this->fs);

case $type->equals(FileType::core()):
return (new ClassFileReader($this->finder))->setFileDirectory(self::APP_CORE_DIR);
return (new ClassFileReader($this->finder, $this->fs))->setFileDirectory(self::APP_CORE_DIR);

case $type->equals(FileType::controller()):
return (new ClassFileReader($this->finder))->setFileDirectory(self::APP_CONTROLLER_DIR);
return (new ClassFileReader($this->finder, $this->fs))->setFileDirectory(self::APP_CONTROLLER_DIR);

case $type->equals(FileType::model()):
return (new ClassFileReader($this->finder))->setFileDirectory(self::APP_MODEL_DIR);
return (new ClassFileReader($this->finder, $this->fs))->setFileDirectory(self::APP_MODEL_DIR);

default:
throw new \InvalidArgumentException("Unsupported file type for {$type->value}");
Expand Down
6 changes: 6 additions & 0 deletions src/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Haemanthus\CodeIgniter3IdeHelper\Writers\FileWriter;
use Psr\Container\ContainerInterface;
use Silly\Application as SillyApplication;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

use function DI\autowire;

Expand All @@ -28,6 +32,8 @@ protected static function definitions(): array
{
return [
FileWriterContract::class => autowire(FileWriter::class),
InputInterface::class => autowire(ArgvInput::class),
OutputInterface::class => autowire(ConsoleOutput::class),
SillyApplication::class => function (ContainerInterface $container): SillyApplication {
$silly = new SillyApplication(Application::APP_NAME, Application::APP_VERSION);
$silly->useContainer($container);
Expand Down
17 changes: 14 additions & 3 deletions src/Readers/FileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Haemanthus\CodeIgniter3IdeHelper\Contracts\FileReader as FileReaderContract;
use Haemanthus\CodeIgniter3IdeHelper\Factories\FileFinderFactory;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

Expand All @@ -21,14 +22,19 @@ abstract class FileReader implements FileReaderContract
*/
protected Finder $finder;

protected Filesystem $fs;

/**
* Undocumented function
*
* @param \Haemanthus\CodeIgniter3IdeHelper\Factories\FileFinderFactory $finder
*/
public function __construct(FileFinderFactory $finder)
{
public function __construct(
FileFinderFactory $finder,
Filesystem $fs
) {
$this->finder = $finder->create();
$this->fs = $fs;
}

/**
Expand Down Expand Up @@ -58,7 +64,7 @@ public function setFileDirectory(string $fileDirectory): self
*/
protected function getFullDirectory(): string
{
return getcwd() . $this->rootDirectory . $this->fileDirectory;
return getcwd() . '/' . $this->rootDirectory . $this->fileDirectory;
}

/**
Expand All @@ -74,6 +80,11 @@ public function setPatterns(array $patterns): self
return $this;
}

public function isDirectoryExists(): bool
{
return $this->fs->exists($this->getFullDirectory());
}

public function getFirstFile(): ?SplFileInfo
{
return $this->getFiles()[0] ?? null;
Expand Down
14 changes: 10 additions & 4 deletions src/Writers/FileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\PrettyPrinter;
use Symfony\Component\Filesystem\Filesystem;

class FileWriter implements FileWriterContract
{
protected ?string $outputPath = null;

protected PrettyPrinter\Standard $printer;

public function __construct(PrettyPrinter\Standard $printer)
{
protected Filesystem $fs;

public function __construct(
PrettyPrinter\Standard $printer,
Filesystem $fs
) {
$this->printer = $printer;
$this->fs = $fs;
}

public function setOutputPath(string $outputPath): self
Expand Down Expand Up @@ -48,7 +54,7 @@ protected function createPropertiesDocumentComment(array $properties): string

protected function getFullPath(): string
{
return getcwd() . $this->outputPath;
return getcwd() . '/' . $this->outputPath;
}

/**
Expand All @@ -66,7 +72,7 @@ public function write(array $classStructuralElements): self

$code = $this->printer->prettyPrintFile($classNodes);

file_put_contents($this->getFullPath(), $code);
$this->fs->dumpFile($this->getFullPath(), $code);

return $this;
}
Expand Down

0 comments on commit 4a69dda

Please sign in to comment.