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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
22 changes: 22 additions & 0 deletions src/Events/DataSavedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace AltDesign\AltRedirect\Events;

use Statamic\Contracts\Git\ProvidesCommitMessage;
use Statamic\Events\Event;
use AltDesign\AltRedirect\Events\IMessage;

class DataSavedEvent extends Event implements ProvidesCommitMessage
{
public $item;

public function __construct(IMessage $item)
{
$this->item = $item;
}

public function commitMessage()
{
return $this->item->getMessage();
}
}
23 changes: 23 additions & 0 deletions src/Events/DataSavedEventMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace AltDesign\AltRedirect\Events;

interface IMessage
{
public function getMessage(): string;
}

class DataSavedEventMessage implements IMessage
{
private string $msg;

public function __construct(string $message)
{
$this->msg = $message;
}

public function getMessage(): string
{
return $this->msg;
}
}
11 changes: 6 additions & 5 deletions src/Helpers/Data.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace AltDesign\AltRedirect\Helpers;

use Illuminate\Support\Facades\File;
Expand Down Expand Up @@ -34,9 +35,9 @@ public function __construct($type, $onlyRegex = false)
// Check redirect folder exists
$this->checkOrMakeDirectories();

if(!$onlyRegex) {
if (!$onlyRegex) {
$allRedirects = [];
foreach($this->types[$this->type] as $path) {
foreach ($this->types[$this->type] as $path) {
$filePath = base_path($path);
$allRedirects = array_merge($allRedirects, File::files($filePath));
}
Expand All @@ -62,16 +63,16 @@ public function __construct($type, $onlyRegex = false)

public function checkOrMakeDirectories()
{
foreach($this->types as $type) {
foreach($type as $directory) {
foreach ($this->types as $type) {
foreach ($type as $directory) {
if (!$this->manager->disk()->exists($directory)) {
$this->manager->disk()->makeDirectory($directory);
}
}
}
}

public function getByKey($key, $value) : array | null
public function getByKey($key, $value): array | null
{
$data = collect($this->data);
$result = $data->firstWhere($key, $value);
Expand Down
16 changes: 12 additions & 4 deletions src/Http/Controllers/AltRedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Statamic\Fields\Blueprint;
use Statamic\Fields\BlueprintRepository;
use Statamic\Filesystem\Manager;
use AltDesign\AltRedirect\Events\DataSavedEvent;
use AltDesign\AltRedirect\Events\DataSavedEventMessage;

class AltRedirectController
{
Expand Down Expand Up @@ -45,7 +47,7 @@ public function index()
$values = $data->all();

// Get a blueprint.So
$blueprint = with(new BlueprintRepository)->setDirectory(__DIR__.'/../../../resources/blueprints')->find($this->type);
$blueprint = with(new BlueprintRepository())->setDirectory(__DIR__.'/../../../resources/blueprints')->find($this->type);
// Get a Fields object
$fields = $blueprint->fields();
// Add the values to the object
Expand Down Expand Up @@ -75,7 +77,7 @@ public function create(Request $request)
$data = new Data($this->type);

// Get a blueprint.
$blueprint = with(new BlueprintRepository)->setDirectory(__DIR__.'/../../../resources/blueprints')->find($this->type);
$blueprint = with(new BlueprintRepository())->setDirectory(__DIR__.'/../../../resources/blueprints')->find($this->type);

// Get a Fields object
$fields = $blueprint->fields();
Expand All @@ -102,6 +104,8 @@ public function create(Request $request)

$data->setAll($fields->process()->values()->toArray());

DataSavedEvent::dispatch(new DataSavedEventMessage('Created redirect'));

$data = new Data($this->type);
$values = $data->all();

Expand All @@ -113,8 +117,8 @@ public function create(Request $request)
public function delete(Request $request)
{
$disk = (new Manager())->disk();
switch($this->type) {
case "redirects" :
switch ($this->type) {
case "redirects":
$disk->delete('content/alt-redirect/' . hash('sha512', base64_encode($request->from)) . '.yaml');
$disk->delete('content/alt-redirect/' . base64_encode($request->from) . '.yaml');
$disk->delete('content/alt-redirect/alt-regex/' . hash('sha512', base64_encode($request->id)) . '.yaml');
Expand All @@ -128,6 +132,8 @@ public function delete(Request $request)
$data = new Data($this->type);
$values = $data->all();

DataSavedEvent::dispatch(new DataSavedEventMessage('Deleted redirect'));

return [
'data' => $values,
];
Expand Down Expand Up @@ -192,6 +198,7 @@ public function import(Request $request)
$data = new Data('redirects');
$data->saveAll($currentData);

DataSavedEvent::dispatch(new DataSavedEventMessage('Imported redirects'));
}

// Toggle a key in a certain item and return the data afterwards
Expand All @@ -213,6 +220,7 @@ public function toggle(Request $request)
}
$item[$toggleKey] = !$item[$toggleKey];
$data->setAll($item);
DataSavedEvent::dispatch(new DataSavedEventMessage('Updated redirect querystring'));
break;
default:
return response('Method not implemented', 500);
Expand Down
34 changes: 23 additions & 11 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php namespace AltDesign\AltRedirect;
<?php

namespace AltDesign\AltRedirect;

use Statamic\Providers\AddonServiceProvider;
use Statamic\Facades\CP\Nav;
use Statamic\Facades\Git;
use Statamic\Facades\Permission;
use Statamic\Filesystem\Manager;
use Statamic\StaticSite\SSG;
use Illuminate\Support\Str;

use AltDesign\AltRedirect\Console\Commands\DefaultQueryStringsCommand;
use AltDesign\AltRedirect\Helpers\DefaultQueryStrings;
use AltDesign\AltRedirect\Helpers\Data;
use AltDesign\AltRedirect\Events\DataSavedEvent;

class ServiceProvider extends AddonServiceProvider
{
Expand All @@ -36,7 +39,7 @@ class ServiceProvider extends AddonServiceProvider
*
* @return self
*/
public function addToNav() : self
public function addToNav(): self
{
Nav::extend(function ($nav) {
$nav->content('Alt Redirect')
Expand All @@ -57,7 +60,7 @@ public function addToNav() : self
*
* @return self
*/
public function registerPermissions() : self
public function registerPermissions(): self
{
Permission::register('view alt-redirect')
->label('View Alt Redirect Settings');
Expand All @@ -70,7 +73,7 @@ public function registerPermissions() : self
*
* @return self
*/
public function registerCommands() : self
public function registerCommands(): self
{
$this->commands([
DefaultQueryStringsCommand::class,
Expand All @@ -83,19 +86,19 @@ public function registerCommands() : self
*
* @return self
*/
public function installDefaultQueryStrings() : self
public function installDefaultQueryStrings(): self
{
// create the standard
if ($this->app->runningInConsole()) {
$disk = (new Manager())->disk();
if (!$disk->exists('content/alt-redirect/.installed')) {
(new DefaultQueryStrings)->makeDefaultQueryStrings();
(new DefaultQueryStrings())->makeDefaultQueryStrings();
}
}
return $this;
}

public function configureSSG() : self
public function configureSSG(): self
{
if (!class_exists(SSG::class)) {
return $this;
Expand All @@ -110,9 +113,10 @@ public function configureSSG() : self
print("Found " . count($redirects) . " redirects\n");

$generated = $directories = 0;
foreach( $redirects as $redirect ) {
foreach ($redirects as $redirect) {
$fromDir = $dest . $redirect['from'];
$from = sprintf('%s%sindex.html',
$from = sprintf(
'%s%sindex.html',
$fromDir,
(Str::endsWith($fromDir, '/') ? '' : '/')
);
Expand All @@ -135,12 +139,20 @@ public function configureSSG() : self
return $this;
}

public function registerEventHandler()
{
if (config('statamic.git.enabled')) {
Git::listen(DataSavedEvent::class);
}
return $this;
}

public function bootAddon()
{
$this->addToNav()
->registerPermissions()
->registerCommands()
->registerEventHandler()
->configureSSG();
}
}