Skip to content

Commit

Permalink
🎨 Remove the need for providing a key on modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Dec 21, 2023
1 parent dd21915 commit f9db4bc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
27 changes: 15 additions & 12 deletions src/Modules/AbstractModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Roots\AcornPretty\Modules;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Roots\Acorn\Application;
use Roots\AcornPretty\Contracts\Module;

Expand All @@ -13,11 +14,6 @@ abstract class AbstractModule implements Module
*/
protected Application $app;

/**
* The module key.
*/
protected string $key = '';

/**
* The module config.
*/
Expand All @@ -28,14 +24,8 @@ abstract class AbstractModule implements Module
*/
public function __construct(Application $app, Collection $config)
{
if (empty($this->key)) {
throw new LifecycleException(
sprintf('Module %s is missing the key property.', get_class($this))
);
}

$this->app = $app;
$this->config = $config->get($this->key);
$this->config = $config->get($this->getKey());

$this->boot();
}
Expand Down Expand Up @@ -71,6 +61,19 @@ protected function enabled(): bool
(! is_admin() || wp_doing_ajax());
}

/**
* Get the module key.
*/
protected function getKey(): string
{
$class = get_class($this);

return Str::of($class)
->afterLast('\\')
->beforeLast('Module')
->snake('-');
}

/**
* Filter a hook handled by a method.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Modules/CleanUpModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

class CleanUpModule extends AbstractModule
{
/**
* The module key.
*/
protected string $key = 'clean-up';

/**
* Handle the module.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Modules/NiceSearchModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

class NiceSearchModule extends AbstractModule
{
/**
* The module key.
*/
protected string $key = 'nice-search';

/**
* The default search endpoint.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Modules/RelativeUrlsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

class RelativeUrlsModule extends AbstractModule
{
/**
* The module key.
*/
protected string $key = 'relative-urls';

/**
* Determine if the module is enabled.
*/
Expand Down

0 comments on commit f9db4bc

Please sign in to comment.