Skip to content

Commit 848e1f3

Browse files
committed
added composer autoload
1 parent 636ab33 commit 848e1f3

File tree

5 files changed

+66
-48
lines changed

5 files changed

+66
-48
lines changed

src/Config/module.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,16 @@
177177

178178
/*
179179
|--------------------------------------------------------------------------
180-
| Choose what module will register as custom namespaces.
181-
| Setting one to false will require you to register that part
182-
| in your own Service Provider class.
180+
| Autoload
183181
|--------------------------------------------------------------------------
182+
|
183+
| Here is the load classes config.
184+
|
184185
*/
185-
'register' => [
186-
'translations' => true,
187-
/**
188-
* load files on boot or register method
189-
*
190-
* @example boot|register
191-
*/
192-
'files' => 'register',
186+
'autoload' => [
187+
'translations' => true,
188+
'files' => 'register',
189+
'composer' => 'register',
193190
],
194191

195192
];

src/Laravel/LaravelFileRepository.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Akaunting\Module\Laravel;
44

55
use Akaunting\Module\FileRepository;
6+
use Akaunting\Module\Laravel\Module;
67

78
class LaravelFileRepository extends FileRepository
89
{

src/Laravel/Module.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Akaunting\Module\Laravel;
44

5+
use Akaunting\Module\Module as BaseModule;
56
use Illuminate\Filesystem\Filesystem;
67
use Illuminate\Foundation\AliasLoader;
78
use Illuminate\Foundation\ProviderRepository;
89
use Illuminate\Support\Str;
9-
use Akaunting\Module\Module as BaseModule;
1010

1111
class Module extends BaseModule
1212
{

src/Lumen/LumenFileRepository.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Akaunting\Module\Lumen;
44

55
use Akaunting\Module\FileRepository;
6+
use Akaunting\Module\Lumen\Module;
67

78
class LumenFileRepository extends FileRepository
89
{

src/Module.php

+55-36
Original file line numberDiff line numberDiff line change
@@ -189,27 +189,48 @@ public function setPath($path)
189189
}
190190

191191
/**
192-
* Bootstrap the application events.
192+
* Register the module.
193+
*/
194+
public function register()
195+
{
196+
$this->autoload('register');
197+
198+
$this->registerAliases();
199+
200+
$this->registerProviders();
201+
202+
$this->fireEvent('register');
203+
}
204+
205+
/**
206+
* Boot the module.
193207
*/
194208
public function boot()
195209
{
196-
if (config('module.register.translations', true) === true) {
197-
$this->registerTranslation();
210+
if (config('module.autoload.translations') === true) {
211+
$this->loadTranslations();
198212
}
199213

200-
if (config('module.register.files') == 'boot') {
201-
$this->registerFiles();
202-
}
214+
$this->autoload('boot');
203215

204216
$this->fireEvent('boot');
205217
}
206218

219+
public function autoload(string $stage): void
220+
{
221+
if (config('module.autoload.files') == $stage) {
222+
$this->loadFiles();
223+
}
224+
225+
if (config('module.autoload.composer') == $stage) {
226+
$this->loadComposer();
227+
}
228+
}
229+
207230
/**
208-
* Register module's translation.
209-
*
210-
* @return void
231+
* Load the translations of this module.
211232
*/
212-
protected function registerTranslation()
233+
protected function loadTranslations(): void
213234
{
214235
$name = $this->getAlias();
215236

@@ -220,6 +241,30 @@ protected function registerTranslation()
220241
}
221242
}
222243

244+
/**
245+
* Load the files from this module.
246+
*/
247+
protected function loadFiles(): void
248+
{
249+
foreach ($this->get('files', []) as $file) {
250+
include $this->getPath() . '/' . $file;
251+
}
252+
}
253+
254+
/**
255+
* Load the composer of this module.
256+
*/
257+
protected function loadComposer(): void
258+
{
259+
$autoload = $this->getPath() . '/vendor/autoload.php';
260+
261+
if (! is_file($autoload)) {
262+
return;
263+
}
264+
265+
include $autoload;
266+
}
267+
223268
/**
224269
* Get json contents from the cache, setting as needed.
225270
*
@@ -264,22 +309,6 @@ public function getComposerAttr($key, $default = null)
264309
return $this->json('composer.json')->get($key, $default);
265310
}
266311

267-
/**
268-
* Register the module.
269-
*/
270-
public function register()
271-
{
272-
$this->registerAliases();
273-
274-
$this->registerProviders();
275-
276-
if (config('module.register.files') == 'register') {
277-
$this->registerFiles();
278-
}
279-
280-
$this->fireEvent('register');
281-
}
282-
283312
/**
284313
* Register the module event.
285314
*
@@ -306,16 +335,6 @@ abstract public function registerProviders();
306335
*/
307336
abstract public function getCachedServicesPath();
308337

309-
/**
310-
* Register the files from this module.
311-
*/
312-
protected function registerFiles()
313-
{
314-
foreach ($this->get('files', []) as $file) {
315-
include $this->path . '/' . $file;
316-
}
317-
}
318-
319338
/**
320339
* Handle call __toString.
321340
*

0 commit comments

Comments
 (0)