Skip to content

Commit

Permalink
laravel-projects/packages/onix automatic push
Browse files Browse the repository at this point in the history
  • Loading branch information
mariojgt committed Oct 21, 2024
1 parent 63eb9b4 commit 66875d0
Show file tree
Hide file tree
Showing 19 changed files with 666 additions and 132 deletions.
3 changes: 0 additions & 3 deletions Publish/Stubs/BaseBladePage.stub

This file was deleted.

28 changes: 28 additions & 0 deletions Publish/Stubs/BaseHtmlPage.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required Meta Tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Document Title, Description, and Author -->
<title>Wave - Bootstrap 5 One Page Template</title>
<meta
name="description"
content="Wave is a Bootstrap 5 One Page Template."
/>
<meta name="author" content="BootstrapBrain" />
<!-- Favicon and Touch Icons -->
<link
rel="icon"
type="image/png"
sizes="512x512"
href="./assets/favicon/favicon-512x512.png"
/>
{{styles}}
</head>

<!-- Body -->
{{html}}

{{scripts}}
</html>
Binary file added Publish/Templates/luxgamers.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions src/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function handle()
'--force' => true,
]);

Artisan::call('install:template luxgamers');

$this->newLine();
$this->info('The command was successful!');
}
Expand Down
114 changes: 114 additions & 0 deletions src/Commands/InstallTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Mariojgt\Onix\Commands;

use Artisan;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Mariojgt\Onix\Model\OnixTemplate;
use Mariojgt\Onix\Database\Seeders\OnixSettingsSeeder;
use Mariojgt\Onix\Database\Seeders\OnixTemplateSeeder;

class InstallTemplate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'install:template {template?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This Command will install Onix template';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// get the template value
$template = $this->argument('template');

if ($template === null) {
// Read all the zip file on the template folder
$templates = [];
$files = File::files(public_path('templates'));
foreach ($files as $file) {
$templates[] = str_replace('.zip', '', $file->getFilename());
}

// reordering the array
$templates = array_values($templates);
$this->info('Please select the template you want to install');
$this->info('----------------------------------------------');
$template = $this->choice('Select the template', $templates);
}

// Unzip the template
$this->info('Unzipping the template');
$zip = new \ZipArchive;
$res = $zip->open(public_path('templates/' . $template . '.zip'));

if ($res === TRUE) {
$zip->extractTo(public_path('templates/'));
$zip->close();
} else {
$this->error('Failed to unzip the template');
}

// Copy the css and js to public folder
$this->info('Copying the template assets');
File::copyDirectory(public_path('templates/' . $template . '/css'), public_path('vendor/Onix/' . $template . '/css'));
File::copyDirectory(public_path('templates/' . $template . '/js'), public_path('vendor/Onix/' . $template . '/js'));

// Move the blocks to the blocks folder
$this->info('Copying the template blocks');
File::copyDirectory(public_path('templates/' . $template . '/blocks/phpblock/'), app_path('Onix/Blocks/'));

$this->info('Copying the html template blocks');
File::copyDirectory(public_path('templates/' . $template . '/blocks/htmlblock/'), resource_path('views/onix/blocks/'));

$this->info('Copying the block icons');
File::copyDirectory(public_path('templates/' . $template . '/blocks/media/'), resource_path('views/onix/media/'));

// Find the seeder TemplateSeeder and run it
$this->info('Running the template seeder');
$seeder = public_path('templates/' . $template . '/template.json');
// Get the class from the $seeder file
$templateInfo = json_decode(file_get_contents($seeder));


$this->info('Copying the template assets');
foreach ($templateInfo->file_copy_paths as $key => $item) {
File::copyDirectory(public_path($item->from), public_path($item->to));
}

OnixTemplate::updateOrCreate([
'slug' => $templateInfo->template->slug,
], [
'name' => $templateInfo->template->name,
'slug' => $templateInfo->template->slug,
'description' => $templateInfo->template->description,
'css_file' => $templateInfo->template->css_file,
'js_file' => $templateInfo->template->js_file,
'use_manifest' => $templateInfo->template->use_manifest,
'preview_image' => $templateInfo->template->preview_image,
]);
}
}
5 changes: 2 additions & 3 deletions src/Controllers/OnixApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ public function getSiteStyles(Request $request)
}
}
} else {
// If the template is not using the manifest file we going to get the css and js files from the database
$cssFiles[] = url($basePath . '/' . $template->css_file);
$jsFiles[] = url($basePath . '/' . $template->js_file);
$cssFiles = $template->getCssFiles();
$jsFiles = $template->getJsFiles();
}

return response()->json([
Expand Down
12 changes: 9 additions & 3 deletions src/Controllers/OnixApiDeployController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,25 @@ public function bladeFilesDeploy(Request $request): bool
$pageHtml = $page->html;
$page->deployed = 1;
$page->save();

$tags = $page->template->getCssAndJsTag();
// Save the page in to a blade file
$replace = [
'variables' => [
'{{html}}'
'{{html}}',
'{{styles}}',
'{{scripts}}',
],
'values' => [
$pageHtml
$pageHtml,
$tags['styles'],
$tags['scripts'],
]
];

// Create the default media file
$this->loadStubFileAndSave(
'BaseBladePage',
'BaseHtmlPage',
resource_path('views/pages/onix'),
$page->slug,
'.blade.php',
Expand Down
75 changes: 75 additions & 0 deletions src/Controllers/OnixTemplateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Mariojgt\Onix\Controllers;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\OnixComponents\HomePage;
use Mariojgt\Onix\Model\OnixPage;
use Mariojgt\Onix\Model\OnixBlock;
use App\Http\Controllers\Controller;
use Mariojgt\Onix\Model\OnixTemplate;
use Mariojgt\Onix\Controllers\OnixApiController;

class OnixTemplateController extends OnixController
{
/**
* @return [blade view]
*/
public function index(Request $request)
{
$templates = OnixTemplate::all();
return view('onix::content.template.index', compact('templates'));
}

/**
* @param Request $request
*
* @return [blade view]
*/
public function edit(Request $request, OnixTemplate $template)
{
$request->validate([
'name' => 'required',
'slug' => 'required|unique:onix_templates,slug,' . $template->id
]);

$template->name = $request->name;
$template->slug = Str::slug($request->slug, '-');
$template->use_manifest = $request->use_manifest == 'on' ? 1 : 0;
$template->css_file = $request->css_path;
$template->js_file = $request->js_path;
$template->save();

return redirect()->back()->with('info', 'Page edited');
}

/**
* @param Request $request
*
* @return [blade view]
*/
public function create(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required|unique:onix_templates,slug'
]);
$template = new OnixTemplate();
$template->name = $request->name;
$template->slug = Str::slug($request->slug, '-');
$template->use_manifest = $request->use_manifest == 'on' ? 1 : 0;
$template->css_file = $request->css_path;
$template->js_file = $request->js_path;
$template->save();

return redirect()->back()->with('info', 'Page edited');
}

public function delete(Request $request, OnixTemplate $template)
{
$template->delete();

return redirect()->back()->with('info', 'Page deleted');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up(): void
$table->string('description')->nullable();
$table->string('css_file')->nullable();
$table->string('js_file')->nullable();
$table->boolean('use_manifest')->default(1);
$table->boolean('use_manifest')->default(0);
$table->string('preview_image')->nullable();
$table->timestamps();
});
Expand Down
70 changes: 69 additions & 1 deletion src/Model/OnixTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Mariojgt\Onix\Model;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class OnixTemplate extends Model
{
Expand All @@ -18,4 +19,71 @@ class OnixTemplate extends Model
'preview_image',
'use_manifest'
];

protected static function boot()
{
parent::boot();
static::created(function ($template) {
// Create a new folder at the public path for the template
$path = public_path('vendor/Onix/' . $template->slug);
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
});
}

public function getCssFiles()
{
$basePath = config('onix.editor_base_path') . '/' . $this->slug;

// If the template is not using the manifest file we going to get the css and js files from the database
$css = explode(',', $this->css_file);
foreach ($css as $key => $item) {
// remove any white space
$item = preg_replace('/\s+/', '', $item);
if (Str::startsWith($item, 'http') || Str::startsWith($item, 'https')) {
$cssFiles[] = $item;
} else {
$cssFiles[] = url($basePath . '/' . $item);
}
}
return $cssFiles;
}

public function getJsFiles()
{
$basePath = config('onix.editor_base_path') . '/' . $this->slug;

// If the template is not using the manifest file we going to get the css and js files from the database
$js = explode(',', $this->js_file);
foreach ($js as $key => $item) {
// remove any white space
$item = preg_replace('/\s+/', '', $item);
// if start with http we going to use the url as is
if (Str::startsWith($item, 'http') || Str::startsWith($item, 'https')) {
$jsFiles[] = $item;
} else {
$jsFiles[] = url($basePath . '/' . $item);
}
}
return $jsFiles;
}

public function getCssAndJsTag()
{
$cssFiles = $this->getCssFiles();
$jsFiles = $this->getJsFiles();
$css = '';
$js = '';
foreach ($cssFiles as $key => $item) {
$css .= '<link rel="stylesheet" href="' . $item . '">';
}
foreach ($jsFiles as $key => $item) {
$js .= '<script src="' . $item . '"></script>';
}
return [
'styles' => $css,
'scripts' => $js
];
}
}
7 changes: 7 additions & 0 deletions src/OnixProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Mariojgt\Onix\Middleware\OnixApi;
use Illuminate\Support\ServiceProvider;
use Mariojgt\Onix\Commands\CreateOnixBlock;
use Mariojgt\Onix\Commands\InstallTemplate;

class OnixProvider extends ServiceProvider
{
Expand All @@ -23,6 +24,7 @@ public function boot()
Republish::class,
Install::class,
CreateOnixBlock::class,
InstallTemplate::class,
]);
}

Expand Down Expand Up @@ -103,5 +105,10 @@ public function publish()
$this->publishes([
__DIR__ . '/../Publish/BladeLayout/' => resource_path('views/components/layout'),
]);

// publish the templates
$this->publishes([
__DIR__ . '/../Publish/Templates/' => public_path('templates/'),
]);
}
}
Loading

0 comments on commit 66875d0

Please sign in to comment.