Skip to content

Commit

Permalink
added activator
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Oct 24, 2023
1 parent 848e1f3 commit d670fd7
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 28 deletions.
127 changes: 127 additions & 0 deletions src/Activators/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Akaunting\Module\Activators;

use Akaunting\Module\Contracts\ActivatorInterface;
use Akaunting\Module\Module;
use Illuminate\Cache\CacheManager as Cache;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;

class File implements ActivatorInterface
{
public Cache $cache;

public Filesystem $files;

public Config $config;

public array $statuses;

public function __construct(Container $app)
{
$this->cache = $app['cache'];
$this->files = $app['files'];
$this->config = $app['config'];
$this->statuses = $this->getStatuses();
}

public function is(Module $module, bool $active): bool
{
if (! isset($this->statuses[$module->getAlias()])) {
$this->setActive($module, $module->get('active', false));
}

return $this->statuses[$module->getAlias()] === $active;
}

public function enable(Module $module): void
{
$this->setActive($module, true);
}

public function disable(Module $module): void
{
$this->setActive($module, false);
}

public function setActive(Module $module, bool $active): void
{
$this->statuses[$module->getAlias()] = $active;

$module->json()->set('active', $active)->save();

$this->writeJson();

$this->flushCache();
}

public function delete(Module $module): void
{
if (! isset($this->statuses[$module->getAlias()])) {
return;
}

unset($this->statuses[$module->getAlias()]);

$this->writeJson();

$this->flushCache();
}

public function reset(): void
{
$path = $this->getFilePath();

if ($this->files->exists($path)) {
$this->files->delete($path);
}

$this->statuses = [];

$this->flushCache();
}

public function getStatuses(): array
{
if (! $this->config->get('module.cache.enabled')) {
return $this->readJson();
}

$key = $this->config->get('module.cache.key') . '.statuses';
$lifetime = $this->config->get('module.cache.lifetime');

return $this->cache->remember($key, $lifetime, function () {
return $this->readJson();
});
}

public function readJson(): array
{
$path = $this->getFilePath();

if (! $this->files->exists($path)) {
return [];
}

return json_decode($this->files->get($path), true);
}

public function writeJson(): void
{
$this->files->put($this->getFilePath(), json_encode($this->statuses, JSON_PRETTY_PRINT));
}

public function flushCache(): void
{
$key = $this->config->get('module.cache.key') . '.statuses';

$this->cache->forget($key);
}

public function getFilePath()
{
return storage_path('module_statuses.json');
}
}
10 changes: 10 additions & 0 deletions src/Config/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,14 @@
'composer' => 'register',
],

/*
|--------------------------------------------------------------------------
| Activator
|--------------------------------------------------------------------------
|
| Here is the activator class.
|
*/
'activator' => env('MODULE_ACTIVATOR', \Akaunting\Module\Activators\File::class),

];
20 changes: 20 additions & 0 deletions src/Contracts/ActivatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Akaunting\Module\Contracts;

use Akaunting\Module\Module;

interface ActivatorInterface
{
public function is(Module $module, bool $active): bool;

public function enable(Module $module): void;

public function disable(Module $module): void;

public function setActive(Module $module, bool $active): void;

public function delete(Module $module): void;

public function reset(): void;
}
47 changes: 22 additions & 25 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Akaunting\Module;

use Akaunting\Module\Contracts\ActivatorInterface;
use Illuminate\Cache\CacheManager;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
Expand Down Expand Up @@ -55,6 +56,11 @@ abstract class Module
*/
private $translator;

/**
* @var ActivatorInterface
*/
private $activator;

/**
* The constructor.
*
Expand All @@ -69,6 +75,7 @@ public function __construct(Container $app, string $alias, $path)
$this->cache = $app['cache'];
$this->files = $app['files'];
$this->translator = $app['translator'];
$this->activator = $app[ActivatorInterface::class];
$this->app = $app;
}

Expand Down Expand Up @@ -347,56 +354,45 @@ public function __toString()

/**
* Determine whether the given status same with the current module status.
*
* @param $status
*
* @return bool
*/
public function isStatus($status) : bool
public function isStatus(bool $status) : bool
{
return $this->get('active', 0) === $status;
return $this->activator->is($this, $status);
}

/**
* Determine whether the current module activated.
*
* @return bool
*/
public function enabled() : bool
{
return $this->isStatus(1);
return $this->activator->is($this, true);
}

/**
* Determine whether the current module not disabled.
*
* @return bool
*/
public function disabled() : bool
{
return !$this->enabled();
return $this->activator->is($this, false);
}

/**
* Set active state for current module.
*
* @param $active
*
* @return bool
*/
public function setActive($active)
public function setActive(bool $active): void
{
return $this->json()->set('active', $active)->save();
$this->activator->setActive($this, $active);
}

/**
* Disable the current module.
*/
public function disable()
public function disable(): void
{
$this->fireEvent('disabling');

$this->setActive(0);
$this->activator->disable($this);

$this->flushCache();

$this->fireEvent('disabled');
Expand All @@ -405,23 +401,24 @@ public function disable()
/**
* Enable the current module.
*/
public function enable()
public function enable(): void
{
$this->fireEvent('enabling');

$this->setActive(1);
$this->activator->enable($this);

$this->flushCache();

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

/**
* Delete the current module.
*
* @return bool
*/
public function delete()
public function delete(): bool
{
$this->activator->delete($this);

return $this->json()->getFilesystem()->deleteDirectory($this->getPath());
}

Expand Down
11 changes: 9 additions & 2 deletions src/Providers/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Akaunting\Module\Providers;

use Akaunting\Module\Contracts\ActivatorInterface;
use Akaunting\Module\Contracts\RepositoryInterface;
use Akaunting\Module\Laravel\LaravelFileRepository;
use Akaunting\Module\Support\Stub;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function setupStubPath()

$this->app->booted(function ($app) {
$repository = $app[RepositoryInterface::class];

if ($repository->config('stubs.enabled') === true) {
Stub::setBasePath($repository->config('stubs.path'));
}
Expand All @@ -56,7 +57,13 @@ protected function registerServices()

return new LaravelFileRepository($app, $path);
});


$this->app->singleton(ActivatorInterface::class, function ($app) {
$class = $app['config']->get('module.activator');

return new $class($app);
});

$this->app->alias(RepositoryInterface::class, 'module');
}

Expand Down
9 changes: 8 additions & 1 deletion src/Providers/Lumen.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Akaunting\Module\Providers;

use Akaunting\Module\Contracts\ActivatorInterface;
use Akaunting\Module\Contracts\RepositoryInterface;
use Akaunting\Module\Lumen\LumenFileRepository;
use Akaunting\Module\Support\Stub;
Expand Down Expand Up @@ -49,7 +50,13 @@ protected function registerServices()

return new LumenFileRepository($app, $path);
});


$this->app->singleton(ActivatorInterface::class, function ($app) {
$class = $app['config']->get('module.activator');

return new $class($app);
});

$this->app->alias(RepositoryInterface::class, 'module');
}
}

0 comments on commit d670fd7

Please sign in to comment.