Skip to content

Commit

Permalink
added composer autoload
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Oct 8, 2023
1 parent 636ab33 commit 848e1f3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 48 deletions.
19 changes: 8 additions & 11 deletions src/Config/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,16 @@

/*
|--------------------------------------------------------------------------
| Choose what module will register as custom namespaces.
| Setting one to false will require you to register that part
| in your own Service Provider class.
| Autoload
|--------------------------------------------------------------------------
|
| Here is the load classes config.
|
*/
'register' => [
'translations' => true,
/**
* load files on boot or register method
*
* @example boot|register
*/
'files' => 'register',
'autoload' => [
'translations' => true,
'files' => 'register',
'composer' => 'register',
],

];
1 change: 1 addition & 0 deletions src/Laravel/LaravelFileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Akaunting\Module\Laravel;

use Akaunting\Module\FileRepository;
use Akaunting\Module\Laravel\Module;

class LaravelFileRepository extends FileRepository
{
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Akaunting\Module\Laravel;

use Akaunting\Module\Module as BaseModule;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Foundation\ProviderRepository;
use Illuminate\Support\Str;
use Akaunting\Module\Module as BaseModule;

class Module extends BaseModule
{
Expand Down
1 change: 1 addition & 0 deletions src/Lumen/LumenFileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Akaunting\Module\Lumen;

use Akaunting\Module\FileRepository;
use Akaunting\Module\Lumen\Module;

class LumenFileRepository extends FileRepository
{
Expand Down
91 changes: 55 additions & 36 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,27 +189,48 @@ public function setPath($path)
}

/**
* Bootstrap the application events.
* Register the module.
*/
public function register()
{
$this->autoload('register');

$this->registerAliases();

$this->registerProviders();

$this->fireEvent('register');
}

/**
* Boot the module.
*/
public function boot()
{
if (config('module.register.translations', true) === true) {
$this->registerTranslation();
if (config('module.autoload.translations') === true) {
$this->loadTranslations();
}

if (config('module.register.files') == 'boot') {
$this->registerFiles();
}
$this->autoload('boot');

$this->fireEvent('boot');
}

public function autoload(string $stage): void
{
if (config('module.autoload.files') == $stage) {
$this->loadFiles();
}

if (config('module.autoload.composer') == $stage) {
$this->loadComposer();
}
}

/**
* Register module's translation.
*
* @return void
* Load the translations of this module.
*/
protected function registerTranslation()
protected function loadTranslations(): void
{
$name = $this->getAlias();

Expand All @@ -220,6 +241,30 @@ protected function registerTranslation()
}
}

/**
* Load the files from this module.
*/
protected function loadFiles(): void
{
foreach ($this->get('files', []) as $file) {
include $this->getPath() . '/' . $file;
}
}

/**
* Load the composer of this module.
*/
protected function loadComposer(): void
{
$autoload = $this->getPath() . '/vendor/autoload.php';

if (! is_file($autoload)) {
return;
}

include $autoload;
}

/**
* Get json contents from the cache, setting as needed.
*
Expand Down Expand Up @@ -264,22 +309,6 @@ public function getComposerAttr($key, $default = null)
return $this->json('composer.json')->get($key, $default);
}

/**
* Register the module.
*/
public function register()
{
$this->registerAliases();

$this->registerProviders();

if (config('module.register.files') == 'register') {
$this->registerFiles();
}

$this->fireEvent('register');
}

/**
* Register the module event.
*
Expand All @@ -306,16 +335,6 @@ abstract public function registerProviders();
*/
abstract public function getCachedServicesPath();

/**
* Register the files from this module.
*/
protected function registerFiles()
{
foreach ($this->get('files', []) as $file) {
include $this->path . '/' . $file;
}
}

/**
* Handle call __toString.
*
Expand Down

0 comments on commit 848e1f3

Please sign in to comment.