Skip to content

Commit

Permalink
Tiding up
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdordoy committed Oct 15, 2024
1 parent 2f8396e commit 50cb9d9
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 25 deletions.
4 changes: 2 additions & 2 deletions config/htmlable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
return [
'default_doctype' => '<!DOCTYPE html>',
'zip_assets' => [
]

],
];
10 changes: 5 additions & 5 deletions src/Actions/Documents/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
namespace JamesDordoy\HTMLable\Actions\Documents;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use JamesDordoy\HTMLable\Models\Document;
use Spatie\TemporaryDirectory\TemporaryDirectory;
use ZipArchive;
use Illuminate\Support\Facades\File;

class Import
{
public function __invoke(ZipArchive $zip, Model $model, bool $validate = true): Document
{
$document = app(Create::class)($model, pathinfo($zip->filename, PATHINFO_FILENAME), '<!DOCTYPE html>');

$temporaryDirectory = (new TemporaryDirectory())->create();
$temporaryDirectory = (new TemporaryDirectory)->create();
$extractPath = $temporaryDirectory->path();
$fullpath = $extractPath . '/' . pathinfo($zip->filename, PATHINFO_FILENAME);
$fullpath = $extractPath.'/'.pathinfo($zip->filename, PATHINFO_FILENAME);

$zip->extractTo($extractPath);
$zip->close();
Expand All @@ -41,7 +41,7 @@ protected function importAssetFolders(Document $document, string $basePath, arra

protected function importAssets(Document $document, string $assetPath)
{
if (!file_exists($assetPath)) {
if (! file_exists($assetPath)) {
return;
}

Expand All @@ -63,4 +63,4 @@ protected function validate(ZipArchive $zip): array
{
return [];
}
}
}
13 changes: 10 additions & 3 deletions src/Actions/RegisterRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@

use Illuminate\Support\Facades\Route;
use JamesDordoy\HTMLable\Http\Controllers\Documents\DocumentsController;
use JamesDordoy\HTMLable\Http\Controllers\Documents\ParseDocumentController;
use JamesDordoy\HTMLable\Http\Controllers\Documents\RenderDocumentController;
use JamesDordoy\HTMLable\Http\Controllers\Elements\ElementsController;
use JamesDordoy\HTMLable\Http\Controllers\Elements\RenderElementController;
use JamesDordoy\HTMLable\Http\Controllers\Media\ServeMediaController;
use JamesDordoy\HTMLable\Http\Controllers\Media\ServeMediaSignedController;
use JamesDordoy\HTMLable\Http\Controllers\ValuesController;
use JamesDordoy\HTMLable\Http\Controllers\Values\ValuesController;

class RegisterRoutes
{
public function __invoke()
{
Route::resource('/htmlable/documents', DocumentsController::class);

Route::get('/htmlable/documents/{document}/render', RenderDocumentController::class);

Route::resource('/htmlable/elements', ElementsController::class);

Route::get('/htmlable/elements/{element}/render', RenderElementController::class);

Route::resource('/htmlable/values', ValuesController::class);
Route::get('/htmlable/media/{media}/serve-signed', ServeMediaSignedController::class)->middleware(['signed'])->name('htmlable.media.serve-signed');

Route::get('/htmlable/media/{media}/serve-signed', ServeMediaSignedController::class)
->middleware(['signed'])
->name('htmlable.media.serve-signed');

Route::get('/htmlable/media/{media}/serve', ServeMediaController::class);
}
}
2 changes: 1 addition & 1 deletion src/Enums/FileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ enum FileType: string
{
case HTML = 'html';
case CSS = 'css';
case JavaScript = 'js';
case JAVASCRIPT = 'js';

}
3 changes: 2 additions & 1 deletion src/Enums/FileTypesToSign.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ enum FileTypesToSign: string
{
case HTML = 'html';
case CSS = 'css';
case JavaScript = 'js';
case JAVASCRIPT = 'js';
case IMG = 'img';
}
1 change: 0 additions & 1 deletion src/Http/Controllers/Media/ServeMediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ public function __invoke(Request $request, Media $media)
return $media->toInlineResponse($request);
}
}

1 change: 0 additions & 1 deletion src/Http/Controllers/Media/ServeMediaSignedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ public function __invoke(Request $request, Media $media)
return $media->toInlineResponse($request);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace JamesDordoy\HTMLable\Http\Controllers;
namespace JamesDordoy\HTMLable\Http\Controllers\Values;

use Illuminate\Http\Request;
use JamesDordoy\HTMLable\Models\Value;
Expand Down
5 changes: 2 additions & 3 deletions src/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function parse(HtmlString|string $html)

$htmlElement = $dom->getElementsByTagName('html')->item(0);

if (!$htmlElement) {
if (! $htmlElement) {
throw new \Exception('cannot parse html document');
}

Expand Down Expand Up @@ -79,7 +79,6 @@ protected function saveElement($node, $parentId = null)
// element: $element
// );


if ($node->nodeName === 'img' && $attr->nodeName === 'src') {
Value::create([
'element_id' => $element->id,
Expand Down Expand Up @@ -112,7 +111,7 @@ protected function saveElement($node, $parentId = null)
foreach ($node->childNodes as $childNode) {
// If the child node is a text node, save its content
if ($childNode->nodeType === XML_TEXT_NODE) {
if (!empty(trim($childNode->nodeValue))) {
if (! empty(trim($childNode->nodeValue))) {
Value::create([
'element_id' => $element->id,
'key' => 'content',
Expand Down
1 change: 0 additions & 1 deletion src/Models/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function renderForDownload()

$attributes = $this->values->map(fn ($value) => $value->renderAttributesLocally())->implode(' ');


$contentValue = $this->values->firstWhere('key', 'content')->value ?? '';

$childrenHtml = '';
Expand Down
12 changes: 6 additions & 6 deletions src/Rules/Documents/CreateDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class CreateDocument
{
public function __invoke(array $additionalRules = []): array
{
return [
return [
...$additionalRules,
'model_id' => ['required', ],
'model_type' => ['required', ],
'name' => ['required', 'unique'],
'doctype' => ['required', 'nullable'],
];
'model_id' => ['required'],
'model_type' => ['required'],
'name' => ['required', 'unique'],
'doctype' => ['required', 'nullable'],
];
}
}
16 changes: 16 additions & 0 deletions src/Rules/Elements/CreateElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace JamesDordoy\HTMLable\Rules\Documents;

class CreateElement
{
public function __invoke(array $additionalRules = []): array
{
return [
...$additionalRules,
'tag' => 'required',
'document_id' => 'required',
'parent_id' => '',
];
}
}
16 changes: 16 additions & 0 deletions src/Rules/Values/CreateValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace JamesDordoy\HTMLable\Rules\Documents;

class CreateElement
{
public function __invoke(array $additionalRules = []): array
{
return [
...$additionalRules,
'key' => 'required',
'value' => 'required|nullable',
'element_id' => 'required',
];
}
}

0 comments on commit 50cb9d9

Please sign in to comment.