Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion app/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Vilare\Blade\Templating;
use Vilare\Templates\Templates;
use Illuminate\Filesystem\Filesystem;
use Vilare\Prettify\CleanUpModule;
use Vilare\Prettify\NiceSearchModule;
use Vilare\Prettify\RelativeUrlsModule;

class App
{
Expand All @@ -30,6 +33,12 @@ class App

private Templating $templating;

private CleanUpModule $cleanUpModule;

private NiceSearchModule $niceSearchModule;

private RelativeUrlsModule $relativeUrlsModule;

private static ?App $instance = null;

private function __construct()
Expand All @@ -45,6 +54,11 @@ private function __construct()
$this->setup = self::init(new Setup());
$this->templates = self::init(new Templates());
$this->templating = self::init(new Templating());
$this->widgets = self::init(new Widgets());
$prettifyConfig = collect($this->config->prettify());
$this->cleanUpModule = self::init(new CleanUpModule($this, $prettifyConfig));
$this->niceSearchModule = self::init(new NiceSearchModule($this, $prettifyConfig));
$this->relativeUrlsModule = self::init(new RelativeUrlsModule($this, $prettifyConfig));
}

public function assets(): Assets
Expand Down Expand Up @@ -87,7 +101,24 @@ public function templating(): Templating
return $this->templating;
}

private function __clone() {}
public function cleanUpModule(): CleanUpModule
{
return $this->cleanUpModule;
}

public function niceSearchModule(): NiceSearchModule
{
return $this->niceSearchModule;
}

public function relativeUrlsModule(): RelativeUrlsModule
{
return $this->relativeUrlsModule;
}

private function __clone()
{
}

public function __wakeup()
{
Expand Down
5 changes: 5 additions & 0 deletions app/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function __construct()
];
}

public function prettify(): array
{
return require __DIR__ . '/../config/prettify.php';
}

public function get(string $key): mixed
{
$value = $this->config;
Expand Down
129 changes: 129 additions & 0 deletions app/Prettify/AbstractModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace Vilare\Prettify;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;

abstract class AbstractModule
{
/**
* The Application instance.
*/
protected $app;

/**
* The module config.
*/
protected Collection $config;

/**
* Initialize the Module instance.
*/
public function __construct(\Vilare\App $app, Collection $config)
{
$this->app = $app;
$this->config = collect($config->get($this->getKey(), []));

$this->boot();
}

/**
* Make a new instance of the module.
*/
public static function make(Application $app, Collection $config): self
{
return new static($app, $config);
}

/**
* Boot the module.
*/
protected function boot(): void
{
if (empty($this->config)) {
return;
}

$method = method_exists($this, 'handle') ? 'handle' : '__invoke';

$this->$method();
}

/**
* Determine if the module is enabled.
*/
protected function enabled(): bool
{
return $this->config->get('enabled', false) &&
(! 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.
*/
protected function filter(string $hook, string $method, int $priority = 10, int $args = 1): self
{
add_filter($hook, [$this, $method], $priority, $args);

return $this;
}

/**
* Remove a hook using a method.
*/
protected function removeFilter(string $hook, string $method, int $priority = 10): self
{
remove_filter($hook, [$this, $method], $priority);

return $this;
}

/**
* Filter multiple hooks by a method.
*/
protected function filters(array $hooks, string $method, int $priority = 10, int $args = 1): self
{
$count = count($hooks);

array_map(
[$this, 'filter'],
(array) $hooks,
array_fill(0, $count, $method),
array_fill(0, $count, $priority),
array_fill(0, $count, $args)
);

return $this;
}

/**
* Remove multiple hooks using a method.
*/
protected function removeFilters(array $hooks, string $method, int $priority = 10): self
{
$count = count($hooks);

array_map(
[$this, 'removeFilter'],
(array) $hooks,
array_fill(0, $count, $method),
array_fill(0, $count, $priority)
);

return $this;
}
}
Loading