Skip to content

Commit

Permalink
feat: add core files reader
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuftaufiq committed Apr 11, 2022
1 parent 60738fa commit f54c0cb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 46 deletions.
14 changes: 9 additions & 5 deletions src/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GenerateCommand
*
* @var string $expression
*/
public static string $expression = 'generate [-d|--dir=] [-c|--controller=]* [-m|--model=]* [-o|--output=]';
public static string $expression = 'generate [--dir=] [--core=]* [--controller=]* [--model=]* [--output=]';

/**
* Undocumented variable
Expand All @@ -32,6 +32,7 @@ class GenerateCommand
*/
public static array $options = [
'--dir' => 'CodeIgniter 3 root directory',
'--core' => 'Pattern in string or regex to match core files',
'--controller' => 'Pattern in string or regex to match controller files',
'--model' => 'Pattern in string or regex to match model files',
'--output' => 'Output filename'
Expand Down Expand Up @@ -78,27 +79,30 @@ public function __construct(
/**
* Undocumented function
*
* @param bool $write
* @param bool $writeMixin
* @param string $dir
* @param string $controllers
* @param string $models
* @param string $core
* @param string $controller
* @param string $model
* @param string $output
*
* @return void
*/
public function __invoke(
$dir = '/./',
$core = [],
$controller = [],
$model = [],
$output = '/./ide-helper.php'
): void {
$this->readerService->setDirectory($dir);

$autoloadFile = $this->readerService->getAutoloadFile();
$coreFiles = $this->readerService->getCoreFiles($core);
$controllerFiles = $this->readerService->getControllerFiles($controller);
$modelFiles = $this->readerService->getModelFiles($model);

var_dump($autoloadFile->getFilename());
var_dump($coreFiles[0]->getContents());
var_dump($controllerFiles[0]->getContents());
var_dump($modelFiles[0]->getContents());
}
Expand Down
41 changes: 1 addition & 40 deletions src/Readers/ControllerReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,12 @@
/**
* Undocumented class
*/
class ControllerReader extends AbstractFileReader
class ControllerReader extends CoreReader
{
/**
* Undocumented variable
*
* @var array
*/
protected array $patterns = [];

/**
* Undocumented variable
*
* @var string $path
*/
protected string $path = './application/controllers/';

/**
* Undocumented function
*
* @param array $patterns
* @return self
*/
public function setPatterns(array $patterns): self
{
$this->patterns = $patterns;

return $this;
}

/**
* Undocumented function
*
* @return array<\Symfony\Component\Finder\SplFileInfo>
*/
public function getFiles(): array
{
array_walk($this->patterns, function ($pattern) {
$this->finder->path($pattern);
});

$this->finder
->files()
->in($this->getFullPath())
->name('*.php');

return iterator_to_array($this->finder, false);
}
}
55 changes: 55 additions & 0 deletions src/Readers/CoreReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Haemanthus\CodeIgniter3IdeHelper\Readers;

/**
* Undocumented class
*/
class CoreReader extends AbstractFileReader
{
/**
* Undocumented variable
*
* @var array
*/
protected array $patterns = [];

/**
* Undocumented variable
*
* @var string $path
*/
protected string $path = './application/core/';

/**
* Undocumented function
*
* @param array $patterns
* @return self
*/
public function setPatterns(array $patterns): self
{
$this->patterns = $patterns;

return $this;
}

/**
* Undocumented function
*
* @return array<\Symfony\Component\Finder\SplFileInfo>
*/
public function getFiles(): array
{
array_walk($this->patterns, function ($pattern) {
$this->finder->path($pattern);
});

$this->finder
->files()
->in($this->getFullPath())
->name('*.php');

return iterator_to_array($this->finder, false);
}
}
2 changes: 1 addition & 1 deletion src/Readers/ModelReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Undocumented class
*/
class ModelReader extends ControllerReader
class ModelReader extends CoreReader
{
/**
* Undocumented variable
Expand Down
25 changes: 25 additions & 0 deletions src/Services/ReaderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Haemanthus\CodeIgniter3IdeHelper\Readers\AutoloadReader;
use Haemanthus\CodeIgniter3IdeHelper\Readers\ControllerReader;
use Haemanthus\CodeIgniter3IdeHelper\Readers\CoreReader;
use Haemanthus\CodeIgniter3IdeHelper\Readers\ModelReader;
use Symfony\Component\Finder\SplFileInfo;

Expand All @@ -19,6 +20,13 @@ class ReaderService
*/
protected AutoloadReader $autoloadReader;

/**
* Undocumented variable
*
* @var \Haemanthus\CodeIgniter3IdeHelper\Readers\CoreReader
*/
protected CoreReader $coreReader;

/**
* Undocumented variable
*
Expand All @@ -37,15 +45,18 @@ class ReaderService
* Undocumented function
*
* @param \Haemanthus\CodeIgniter3IdeHelper\Readers\AutoloadReader $autoloadReader
* @param \Haemanthus\CodeIgniter3IdeHelper\Readers\CoreReader $coreReader
* @param \Haemanthus\CodeIgniter3IdeHelper\Readers\ControllerReader $controllerReader
* @param \Haemanthus\CodeIgniter3IdeHelper\Readers\ModelReader $modelReader
*/
public function __construct(
AutoloadReader $autoloadReader,
CoreReader $coreReader,
ControllerReader $controllerReader,
ModelReader $modelReader
) {
$this->autoloadReader = $autoloadReader;
$this->coreReader = $coreReader;
$this->controllerReader = $controllerReader;
$this->modelReader = $modelReader;
}
Expand All @@ -59,6 +70,7 @@ public function __construct(
public function setDirectory(string $dir): self
{
$this->autoloadReader->setDirectory($dir);
$this->coreReader->setDirectory($dir);
$this->controllerReader->setDirectory($dir);
$this->modelReader->setDirectory($dir);

Expand All @@ -75,6 +87,19 @@ public function getAutoloadFile(): SplFileInfo
return $this->autoloadReader->getFiles()[0];
}

/**
* Undocumented function
*
* @param array $patterns
* @return array<\Symfony\Component\Finder\SplFileInfo>
*/
public function getCoreFiles(array $patterns): array
{
return $this->coreReader
->setPatterns($patterns)
->getFiles();
}

/**
* Undocumented function
*
Expand Down

0 comments on commit f54c0cb

Please sign in to comment.