Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat: use autoloader instead of include
Browse files Browse the repository at this point in the history
  • Loading branch information
AlxisHenry committed Feb 11, 2023
1 parent 2b64621 commit 43e4815
Show file tree
Hide file tree
Showing 28 changed files with 565 additions and 496 deletions.
37 changes: 37 additions & 0 deletions app/Classes/Asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Classes;

class Asset {

/**
* @param string|array<string> $f
* @return string
*/
public static function new(string|array $f): string
{
$html = "";
if (is_array($f)) {
foreach ($f as $file) {
$html .= self::generateAssetHtmlTag(filename: $file);
}
} else {
$html .= self::generateAssetHtmlTag(filename: $f);
}
return $html;
}

/**
* @param string $filename
* @return string
*/
private static function generateAssetHtmlTag(string $filename): string
{
$file = (($_SERVER['HTTPS'] ?? false) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . "/$filename";
if (pathinfo($file, PATHINFO_EXTENSION) === "js") return "<script src='$file' async></script>";
return "<link href='$file' rel='stylesheet'/>";
}

}
2 changes: 2 additions & 0 deletions app/Classes/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

namespace App\Classes;

use Symfony\Component\Yaml\Yaml;

class Auth {
Expand Down
89 changes: 89 additions & 0 deletions app/Classes/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace App\Classes;

use App\Classes\Item;

class Category {

/**
* Return an array with the differents categories
*
* @return array<string>
*/
public static function all(): array
{
$categories = [];
$directories = scandir('./shared') ?: [];
$exclude = ['.', '..', 'build', 'index.php'];
foreach ($directories as $directory) {
if (!in_array($directory, $exclude)) $categories[] = $directory;
}
return $categories;
}

/**
* Return an array with the items of a category passed in parameter
*
* @param string $category
* @return array<int, array<string,int|string|false>>
*/
public static function items(string $category): array
{
$files = [];
$categoryItems = scandir("./shared/$category/") ?: [];
$exclude = ['.', '..', '.gitignore', 'index.php'];
foreach ($categoryItems as $item) {
if (!in_array($item, $exclude)) {
$files[] = [
'category' => $category,
'filename' => $item,
'updated_at' => filemtime("./shared/$category/$item")
];
}
}
return $files;
}

/**
* Generate list of items of a given category
*
* @param string $category
* @return string
*/
public static function list(string $category): string
{
$items = self::items($category);
$list = Item::create($items, html: true);
return $list;
}

/**
* Generate HTML of the section of a given category
*
* @param string $category
* @param bool $background
* @return string
*/
public static function section(string $category, bool $background): string
{
$sectionClass = $background ? "bg-primary-light text-white mb-0" : "";
$titleClass = $background ? "text-white" : "";
$dividerClass = $background ? "divider-light" : "";
return "<section class='page-section $sectionClass' id='$category'>
<div class='container'>
<h2 class='page-section-heading text-center text-uppercase $titleClass'>".ucfirst($category)."</h2>
<!-- Icon Divider-->
<div class='divider-custom $dividerClass'>
<div class='divider-custom-line'></div>
<div class='divider-custom-icon'><i class='fas fa-star'></i></div>
<div class='divider-custom-line'></div>
</div>
".self::list($category)."
</div>
</section>";
}

}
3 changes: 3 additions & 0 deletions app/Classes/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

declare(strict_types=1);

namespace App\Classes;

use Exception;
use Symfony\Component\Yaml\Yaml;

class Dashboard {
Expand Down
2 changes: 2 additions & 0 deletions app/Classes/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

namespace App\Classes;

class File {

/**
Expand Down
176 changes: 176 additions & 0 deletions app/Classes/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

declare(strict_types=1);

namespace App\Classes;

use Exception;
use ZipArchive;

class Helper {

/**
* @param string $element
* @param string $type
* @return string
*/
public static function htmlFormat(string $element, string $type = "ul"): string
{
return "<$type class='list-group'>$element</$type>";
}

/**
* Show a sweet alert message if the cookie is set or if you specify ²0
*
* @param ?string $type
* @return string
*/
public static function swal(): string
{
$swal = $_COOKIE['swal'] ?? null;
if ($swal === "logout") session_destroy();
return match ($swal) {
'connected' => "<script>
Swal.fire({
icon: 'success',
iconColor: '#464aa6',
title: 'Connected',
text: 'You are now connected to the dashboard',
showConfirmButton: true,
timerProgressBar: true,
timer: 3000
})
</script>",
'connection_failed' => "<script>
Swal.fire({
icon: 'error',
iconColor: '#242b40',
title: 'Connection failed',
text: 'The username or password is incorrect',
showConfirmButton: true,
timerProgressBar: true,
timer: 3000
})
</script>",
'logout' => "<script>
Swal.fire({
icon: 'success',
iconColor: '#464aa6',
title: 'Logout',
text: 'You are now disconnected from the dashboard',
showConfirmButton: true,
timerProgressBar: true,
timer: 3000
})
</script>",
'file_renamed' => "<script>
Swal.fire({
icon: 'success',
iconColor: '#464aa6',
title: 'File renamed',
text: 'The file has been renamed',
showConfirmButton: true,
timerProgressBar: true,
timer: 3000
})
</script>",
'file_uploaded' => "<script>
Swal.fire({
icon: 'success',
iconColor: '#464aa6',
title: 'File(s) uploaded',
text: 'File(s) have been uploaded',
showConfirmButton: true,
timerProgressBar: true,
timer: 3000
})
</script>",
'file_upload_failed' => "<script>
Swal.fire({
icon: 'error',
iconColor: '#242b40',
title: 'File(s) upload failed',
text: 'Files could not be uploaded',
showConfirmButton: true,
timerProgressBar: true,
timer: 3000
})
</script>",
'files_not_send' => "<script>
Swal.fire({
icon: 'error',
iconColor: '#242b40',
title: 'Upload failed',
text: 'Please select at least one file to upload',
showConfirmButton: true,
timerProgressBar: true,
timer: 3000
})
</script>",
default => '',
};
}

/**
* @param array<string> $files
* @throws Exception
* @return void
*/
public static function createZipAndDownload(array $files): void
{

$zip = new ZipArchive();

$zipFilename = "cdn-" . time() . ".zip";
$zipFilepath = "./archives";
$zipFile = "$zipFilepath/$zipFilename";

if (!$zip->open($zipFile, ZipArchive::CREATE)) throw new Exception('Could not open zip file.');

// Adding files to the zip
foreach ($files as $file) {
$file = ".$file";
if (file_exists($file)) $zip->addFile($file, basename($file));
}

$zip->close();

// Download the created zip file
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipFilename);
header('Content-Length: ' . filesize($zipFile));
readfile($zipFile);

unlink($zipFile);

die();
}

/**
* @param string $key
* @throws Exception
* @return string
*/
public static function config(string $key): string
{
$config = require __DIR__ . '/../../config.php';
if (isset($config[$key])) return $config[$key];
throw new Exception("The key $key does not exist in the config file.");
}

/**
* @param string $size
* @param bool $round
* @throws Exception
* @return string
*/
public static function formatFilesize(string $size, bool $round = true): string
{
$b = $round ? 1000 : 1024;
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
for ($i = 0; $size > $b; $i++) $size /= $b;
/** @phpstan-ignore-next-line */
return round($size, 2) . ' ' . $units[$i];
}

}
Loading

0 comments on commit 43e4815

Please sign in to comment.