Skip to content

Commit

Permalink
fix: add default autoload libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuftaufiq committed May 24, 2022
1 parent a9cb1a5 commit 7e094c1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Casters/LibraryNameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ class LibraryNameMapper implements FileNameMapper
* @var array<string, string>
*/
protected $mapLibraries = [
'benchmark' => 'CI_Benchmark',
'cache' => 'CI_Cache',
'calendar' => 'CI_Calendar',
'config' => 'CI_Config',
'db' => 'CI_DB',
'driver' => 'CI_Driver',
'email' => 'CI_Email',
'encryption' => 'CI_Encryption',
'form_validation' => 'CI_Form_validation',
'ftp' => 'CI_Ftp',
'image_lib' => 'CI_Image_lib',
'input' => 'CI_Input',
'lang' => 'CI_Lang',
'migration' => 'CI_Migration',
'output' => 'CI_Output',
'pagination' => 'CI_Pagination',
'parser' => 'CI_Parser',
'profiler' => 'CI_Profiler',
'security' => 'CI_Security',
'session' => 'CI_Session',
'table' => 'CI_Table',
'trackback' => 'CI_Trackback',
'typography' => 'CI_Typography',
'unit_test' => 'CI_Unit_test',
'upload' => 'CI_Upload',
'uri' => 'CI_URI',
'user_agent' => 'CI_User_agent',
'xmlrpc' => 'CI_Xmlrpc',
'xmlrpcs' => 'CI_Xmlrpcs',
Expand Down
33 changes: 31 additions & 2 deletions src/Parsers/AutoloadFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Haemanthus\CodeIgniter3IdeHelper\Parsers;

use Haemanthus\CodeIgniter3IdeHelper\Casters\LibraryNameMapper;
use Haemanthus\CodeIgniter3IdeHelper\Contracts\NodeCaster;
use Haemanthus\CodeIgniter3IdeHelper\Contracts\NodeVisitor;
use Haemanthus\CodeIgniter3IdeHelper\Elements\ClassStructuralElement;
use Haemanthus\CodeIgniter3IdeHelper\Elements\PropertyStructuralElement;
use Haemanthus\CodeIgniter3IdeHelper\Enums\NodeVisitorType;
use Haemanthus\CodeIgniter3IdeHelper\Factories\NodeCasterFactory;
use Haemanthus\CodeIgniter3IdeHelper\Factories\NodeTraverserFactory;
Expand All @@ -15,6 +17,18 @@

class AutoloadFileParser extends FileParser
{
protected array $defaultAutoloadLibraries = [
'benchmark',
'cache',
'config',
'db',
'input',
'lang',
'output',
'security',
'uri',
];

protected NodeVisitor $autoloadLibraryNodeVisitor;

protected NodeVisitor $autoloadModelNodeVisitor;
Expand Down Expand Up @@ -73,6 +87,15 @@ protected function parseAutoloadModelNodes(array $nodes): array
), []);
}

protected function getDefaultAutoloadLibraries(): array
{
$mapper = new LibraryNameMapper();

return array_map(fn (string $library): PropertyStructuralElement => (
new PropertyStructuralElement($library, $mapper->concreteFileNameOf($library))
), $this->defaultAutoloadLibraries);
}

public function parse(string $contents): array
{
$this->traverser->traverse($this->parser->parse($contents));
Expand All @@ -85,14 +108,20 @@ public function parse(string $contents): array
$this->autoloadModelNodeVisitor->getFoundNodes()
);

$propertyStructuralElements = array_merge(
$loadLibraryStructuralElements,
$loadModelStructuralElements,
$this->getDefaultAutoloadLibraries(),
);

$controllerClassStructuralElement = new ClassStructuralElement(
$this->nodeBuilder->class('CI_Controller')->getNode(),
array_merge($loadLibraryStructuralElements, $loadModelStructuralElements),
$propertyStructuralElements,
);

$modelClassStructuralElement = new ClassStructuralElement(
$this->nodeBuilder->class('CI_Model')->getNode(),
array_merge($loadLibraryStructuralElements, $loadModelStructuralElements),
$propertyStructuralElements,
);

return [$controllerClassStructuralElement, $modelClassStructuralElement];
Expand Down

0 comments on commit 7e094c1

Please sign in to comment.