-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d82f4f
commit 7124b31
Showing
5 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |