Skip to content

Commit d670fd7

Browse files
committed
added activator
1 parent 848e1f3 commit d670fd7

File tree

6 files changed

+196
-28
lines changed

6 files changed

+196
-28
lines changed

src/Activators/File.php

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Akaunting\Module\Activators;
4+
5+
use Akaunting\Module\Contracts\ActivatorInterface;
6+
use Akaunting\Module\Module;
7+
use Illuminate\Cache\CacheManager as Cache;
8+
use Illuminate\Config\Repository as Config;
9+
use Illuminate\Container\Container;
10+
use Illuminate\Filesystem\Filesystem;
11+
12+
class File implements ActivatorInterface
13+
{
14+
public Cache $cache;
15+
16+
public Filesystem $files;
17+
18+
public Config $config;
19+
20+
public array $statuses;
21+
22+
public function __construct(Container $app)
23+
{
24+
$this->cache = $app['cache'];
25+
$this->files = $app['files'];
26+
$this->config = $app['config'];
27+
$this->statuses = $this->getStatuses();
28+
}
29+
30+
public function is(Module $module, bool $active): bool
31+
{
32+
if (! isset($this->statuses[$module->getAlias()])) {
33+
$this->setActive($module, $module->get('active', false));
34+
}
35+
36+
return $this->statuses[$module->getAlias()] === $active;
37+
}
38+
39+
public function enable(Module $module): void
40+
{
41+
$this->setActive($module, true);
42+
}
43+
44+
public function disable(Module $module): void
45+
{
46+
$this->setActive($module, false);
47+
}
48+
49+
public function setActive(Module $module, bool $active): void
50+
{
51+
$this->statuses[$module->getAlias()] = $active;
52+
53+
$module->json()->set('active', $active)->save();
54+
55+
$this->writeJson();
56+
57+
$this->flushCache();
58+
}
59+
60+
public function delete(Module $module): void
61+
{
62+
if (! isset($this->statuses[$module->getAlias()])) {
63+
return;
64+
}
65+
66+
unset($this->statuses[$module->getAlias()]);
67+
68+
$this->writeJson();
69+
70+
$this->flushCache();
71+
}
72+
73+
public function reset(): void
74+
{
75+
$path = $this->getFilePath();
76+
77+
if ($this->files->exists($path)) {
78+
$this->files->delete($path);
79+
}
80+
81+
$this->statuses = [];
82+
83+
$this->flushCache();
84+
}
85+
86+
public function getStatuses(): array
87+
{
88+
if (! $this->config->get('module.cache.enabled')) {
89+
return $this->readJson();
90+
}
91+
92+
$key = $this->config->get('module.cache.key') . '.statuses';
93+
$lifetime = $this->config->get('module.cache.lifetime');
94+
95+
return $this->cache->remember($key, $lifetime, function () {
96+
return $this->readJson();
97+
});
98+
}
99+
100+
public function readJson(): array
101+
{
102+
$path = $this->getFilePath();
103+
104+
if (! $this->files->exists($path)) {
105+
return [];
106+
}
107+
108+
return json_decode($this->files->get($path), true);
109+
}
110+
111+
public function writeJson(): void
112+
{
113+
$this->files->put($this->getFilePath(), json_encode($this->statuses, JSON_PRETTY_PRINT));
114+
}
115+
116+
public function flushCache(): void
117+
{
118+
$key = $this->config->get('module.cache.key') . '.statuses';
119+
120+
$this->cache->forget($key);
121+
}
122+
123+
public function getFilePath()
124+
{
125+
return storage_path('module_statuses.json');
126+
}
127+
}

src/Config/module.php

+10
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,14 @@
189189
'composer' => 'register',
190190
],
191191

192+
/*
193+
|--------------------------------------------------------------------------
194+
| Activator
195+
|--------------------------------------------------------------------------
196+
|
197+
| Here is the activator class.
198+
|
199+
*/
200+
'activator' => env('MODULE_ACTIVATOR', \Akaunting\Module\Activators\File::class),
201+
192202
];

src/Contracts/ActivatorInterface.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Akaunting\Module\Contracts;
4+
5+
use Akaunting\Module\Module;
6+
7+
interface ActivatorInterface
8+
{
9+
public function is(Module $module, bool $active): bool;
10+
11+
public function enable(Module $module): void;
12+
13+
public function disable(Module $module): void;
14+
15+
public function setActive(Module $module, bool $active): void;
16+
17+
public function delete(Module $module): void;
18+
19+
public function reset(): void;
20+
}

src/Module.php

+22-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Akaunting\Module;
44

5+
use Akaunting\Module\Contracts\ActivatorInterface;
56
use Illuminate\Cache\CacheManager;
67
use Illuminate\Container\Container;
78
use Illuminate\Filesystem\Filesystem;
@@ -55,6 +56,11 @@ abstract class Module
5556
*/
5657
private $translator;
5758

59+
/**
60+
* @var ActivatorInterface
61+
*/
62+
private $activator;
63+
5864
/**
5965
* The constructor.
6066
*
@@ -69,6 +75,7 @@ public function __construct(Container $app, string $alias, $path)
6975
$this->cache = $app['cache'];
7076
$this->files = $app['files'];
7177
$this->translator = $app['translator'];
78+
$this->activator = $app[ActivatorInterface::class];
7279
$this->app = $app;
7380
}
7481

@@ -347,56 +354,45 @@ public function __toString()
347354

348355
/**
349356
* Determine whether the given status same with the current module status.
350-
*
351-
* @param $status
352-
*
353-
* @return bool
354357
*/
355-
public function isStatus($status) : bool
358+
public function isStatus(bool $status) : bool
356359
{
357-
return $this->get('active', 0) === $status;
360+
return $this->activator->is($this, $status);
358361
}
359362

360363
/**
361364
* Determine whether the current module activated.
362-
*
363-
* @return bool
364365
*/
365366
public function enabled() : bool
366367
{
367-
return $this->isStatus(1);
368+
return $this->activator->is($this, true);
368369
}
369370

370371
/**
371372
* Determine whether the current module not disabled.
372-
*
373-
* @return bool
374373
*/
375374
public function disabled() : bool
376375
{
377-
return !$this->enabled();
376+
return $this->activator->is($this, false);
378377
}
379378

380379
/**
381380
* Set active state for current module.
382-
*
383-
* @param $active
384-
*
385-
* @return bool
386381
*/
387-
public function setActive($active)
382+
public function setActive(bool $active): void
388383
{
389-
return $this->json()->set('active', $active)->save();
384+
$this->activator->setActive($this, $active);
390385
}
391386

392387
/**
393388
* Disable the current module.
394389
*/
395-
public function disable()
390+
public function disable(): void
396391
{
397392
$this->fireEvent('disabling');
398393

399-
$this->setActive(0);
394+
$this->activator->disable($this);
395+
400396
$this->flushCache();
401397

402398
$this->fireEvent('disabled');
@@ -405,23 +401,24 @@ public function disable()
405401
/**
406402
* Enable the current module.
407403
*/
408-
public function enable()
404+
public function enable(): void
409405
{
410406
$this->fireEvent('enabling');
411407

412-
$this->setActive(1);
408+
$this->activator->enable($this);
409+
413410
$this->flushCache();
414411

415412
$this->fireEvent('enabled');
416413
}
417414

418415
/**
419416
* Delete the current module.
420-
*
421-
* @return bool
422417
*/
423-
public function delete()
418+
public function delete(): bool
424419
{
420+
$this->activator->delete($this);
421+
425422
return $this->json()->getFilesystem()->deleteDirectory($this->getPath());
426423
}
427424

src/Providers/Laravel.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Akaunting\Module\Providers;
44

5+
use Akaunting\Module\Contracts\ActivatorInterface;
56
use Akaunting\Module\Contracts\RepositoryInterface;
67
use Akaunting\Module\Laravel\LaravelFileRepository;
78
use Akaunting\Module\Support\Stub;
@@ -39,7 +40,7 @@ public function setupStubPath()
3940

4041
$this->app->booted(function ($app) {
4142
$repository = $app[RepositoryInterface::class];
42-
43+
4344
if ($repository->config('stubs.enabled') === true) {
4445
Stub::setBasePath($repository->config('stubs.path'));
4546
}
@@ -56,7 +57,13 @@ protected function registerServices()
5657

5758
return new LaravelFileRepository($app, $path);
5859
});
59-
60+
61+
$this->app->singleton(ActivatorInterface::class, function ($app) {
62+
$class = $app['config']->get('module.activator');
63+
64+
return new $class($app);
65+
});
66+
6067
$this->app->alias(RepositoryInterface::class, 'module');
6168
}
6269

src/Providers/Lumen.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Akaunting\Module\Providers;
44

5+
use Akaunting\Module\Contracts\ActivatorInterface;
56
use Akaunting\Module\Contracts\RepositoryInterface;
67
use Akaunting\Module\Lumen\LumenFileRepository;
78
use Akaunting\Module\Support\Stub;
@@ -49,7 +50,13 @@ protected function registerServices()
4950

5051
return new LumenFileRepository($app, $path);
5152
});
52-
53+
54+
$this->app->singleton(ActivatorInterface::class, function ($app) {
55+
$class = $app['config']->get('module.activator');
56+
57+
return new $class($app);
58+
});
59+
5360
$this->app->alias(RepositoryInterface::class, 'module');
5461
}
5562
}

0 commit comments

Comments
 (0)