Skip to content

Commit

Permalink
feat: add file writer
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuftaufiq committed May 20, 2022
1 parent 0d82f4f commit 7124b31
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Commands/GenerateHelperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public function execute(): void
public function __invoke(
string $dir = '/./',
array $pattern = [],
string $output = '/./ide-helper.php'
string $output = '/./_ide_helper.php'
) {
$this->facade
->withDirectory($dir)
->withPatterns($pattern)
->withOutput($output)
->generate();
}
}
10 changes: 10 additions & 0 deletions src/Contracts/FileWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Haemanthus\CodeIgniter3IdeHelper\Contracts;

interface FileWriter
{
public function setOutputPath(string $outputPath): self;

public function write(array $classStructuralElements): self;
}
17 changes: 15 additions & 2 deletions src/Facades/GenerateHelperFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

namespace Haemanthus\CodeIgniter3IdeHelper\Facades;

use Haemanthus\CodeIgniter3IdeHelper\Contracts\FileWriter;

class GenerateHelperFacade
{
protected ReaderFacade $reader;

protected ParserFacade $parser;

protected FileWriter $writer;

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

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

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

return $this;
}

public function generate(): void
{
$structuralElements = array_merge(
$this->parser->parseAutoloadFile($this->reader->getAutoloadFile()),
$this->parser->parseClassFiles($this->reader->getClassFiles()),
);

dd($structuralElements);
$this->writer->write($structuralElements);
}
}
5 changes: 5 additions & 0 deletions src/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
use DI\ContainerBuilder;
use Haemanthus\CodeIgniter3IdeHelper\Application;
use Haemanthus\CodeIgniter3IdeHelper\Commands\StartVarDumperCommand;
use Haemanthus\CodeIgniter3IdeHelper\Contracts\FileWriter as FileWriterContract;
use Haemanthus\CodeIgniter3IdeHelper\Writers\FileWriter;
use Psr\Container\ContainerInterface;
use Silly\Application as SillyApplication;

use function DI\autowire;

/**
* Undocumented class
*/
Expand All @@ -23,6 +27,7 @@ class AppServiceProvider
protected static function definitions(): array
{
return [
FileWriterContract::class => autowire(FileWriter::class),
SillyApplication::class => function (ContainerInterface $container): SillyApplication {
$silly = new SillyApplication(Application::APP_NAME, Application::APP_VERSION);
$silly->useContainer($container);
Expand Down
73 changes: 73 additions & 0 deletions src/Writers/FileWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Haemanthus\CodeIgniter3IdeHelper\Writers;

use Haemanthus\CodeIgniter3IdeHelper\Contracts\FileWriter as FileWriterContract;
use Haemanthus\CodeIgniter3IdeHelper\Elements\ClassStructuralElement;
use Haemanthus\CodeIgniter3IdeHelper\Elements\PropertyStructuralElement;
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\PrettyPrinter;

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

protected PrettyPrinter\Standard $printer;

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

public function setOutputPath(string $outputPath): self
{
$this->outputPath = $outputPath;

return $this;
}

protected function createClassNodeWithDocumentComment(ClassStructuralElement $classStructuralElement): Node
{
$documentComment = $this->createPropertiesDocumentComment($classStructuralElement->getProperties());
$class = $classStructuralElement->getNode();

$class->setDocComment(new Comment\Doc($documentComment));

return $class;
}

protected function createPropertiesDocumentComment(array $properties): string
{
$tags = array_map(fn (PropertyStructuralElement $property): string => (
" * @property {$property->getType()} \${$property->getName()}"
), $properties);

return "/**\n" . implode("\n", $tags) . "\n */";
}

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

/**
* Undocumented function
*
* @param array<ClassStructuralElement> $classStructuralElements
*
* @return self
*/
public function write(array $classStructuralElements): self
{
$classNodes = array_map(fn (ClassStructuralElement $classStructuralElement): Node => (
$this->createClassNodeWithDocumentComment($classStructuralElement)
), $classStructuralElements);

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

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

return $this;
}
}

0 comments on commit 7124b31

Please sign in to comment.