Praca, praca

Took 5 hours 39 minutes
This commit is contained in:
Krzysztof 'KrzysiekSiemv' Smaga
2023-05-14 23:21:51 +02:00
parent 36302fbaf1
commit 1bb536cd12
22 changed files with 502 additions and 52 deletions
+157 -24
View File
@@ -1,9 +1,12 @@
<?php
require "vendor/blog-engine/php/Dictionary.php";
if(file_exists("_config.php")) {
require "vendor/blog-engine/php/Dictionary.php";
}
class CompileIt{
public \BlogEngine\Dictionary $dictionary;
public function __construct(public $src = __DIR__ . "/src/", public $htmls = [], public $csss = [], public $jss = [], public $version = 0){
$this->dictionary = new BlogEngine\Dictionary();
public function __construct(public $src = __DIR__ . "/src/", public $htmls = [], public $csss = [], public $jss = [], public float $version = 0){
if(file_exists("_config.php"))
$this->dictionary = new BlogEngine\Dictionary();
// POBIERANIE ZAWARTOŚCI FOLDERU src
$this->src = __DIR__ . "/src/";
@@ -11,28 +14,77 @@
$this->csss = scandir($src . "css");
$this->jss = scandir($src . "js");
//$this->version =
$this->version = floatval(file_get_contents("version"));
}
public function main() : void{
$this->checkFolders();
$this->compileFiles();
echo "Done";
public function main($argv, $argc) : void{
if($argc > 1){
switch($argv[1]){
case "compile":
if($argv[2] != null)
switch($argv[2]) {
case "dev":
echo "
==========================
= KOMPILOWANIE DO 'dev'
==========================
";
$this->versionUp("dev");
$this->addCustoms("dev");
$this->checkFolders("dev");
$this->compileFiles("dev");
break;
default:
echo "
==========================
= KOMPILOWANIE DO 'pub'
==========================
";
$this->versionUp("pub");
$this->addCustoms("pub");
$this->checkFolders("public");
$this->compileFiles("public");
break;
}
else {
echo "
==========================
= KOMPILOWANIE DO 'pub'
==========================
";
$this->versionUp("pub");
$this->addCustoms("pub");
$this->checkFolders("public");
$this->compileFiles("public");
}
break;
case "start-server":
if($argv[2] != null)
$this->runServer($argv[2]);
else
$this->runServer("pub");
break;
case "help":
$this->help();
break;
}
} else {
echo "Brak parametru rozruchowego. Wpisz help, aby zobaczyć listę dostępnych komend. \n";
}
}
/*
* SPRAWDZANIE CZY SĄ FOLDERY css I js
*/
function checkFolders() : void{
if(!file_exists("public/css"))
mkdir("public/css");
function checkFolders($folder) : void{
if(!file_exists("$folder/css"))
mkdir("$folder/css");
if(!file_exists("public/js"))
mkdir("public/js");
if(!file_exists("$folder/js"))
mkdir("$folder/js");
}
function compileFiles() : void{
function compileFiles($folder) : void{
// KOPIOWANIE PLIKÓW HTML DO PUBLICZNEGO FOLDERU
foreach ($this->htmls as $html) {
$file = $this->src . "views/" . $html;
@@ -40,25 +92,26 @@
$name = basename($file);
$site = file_get_contents($file);
foreach (array_keys($this->dictionary->page_tags) as $page_tag) {
echo $page_tag;
if ($page_tag == "{CSS}") {
$this->dictionary->page_tags[$page_tag] .= "<link rel='stylesheet' href='vendor/blog-engine/blog.css' />";
for ($j = 2; $j < count($this->csss); $j++)
$this->dictionary->page_tags[$page_tag] .= "\n <link rel='stylesheet' href='public/css/{$this->csss[$j]}' />";
$this->dictionary->page_tags[$page_tag] .= "\n <link rel='stylesheet' href='$folder/css/{$this->csss[$j]}?v{$this->version}' />";
}
if ($page_tag == "{JS}") {
$this->dictionary->page_tags[$page_tag] .= "<script src='vendor/blog-engine/blog.js'></script>";
for ($j = 2; $j < count($this->jss); $j++)
$this->dictionary->page_tags[$page_tag] .= "\n <script src='public/js/{$this->jss[$j]}'></script>";
$this->dictionary->page_tags[$page_tag] .= "\n <script src='$folder/js/{$this->jss[$j]}?v{$this->version}'></script>";
}
$site = str_replace($page_tag, $this->dictionary->page_tags[$page_tag], $site);
}
$site_file = fopen("public/" . $name, "w");
$site = str_replace("<body>", "<body>\n<?php include \$topbar; ?>", $site);
$site_file = fopen("$folder/" . $name, "w");
fwrite($site_file, $site);
fclose($site_file);
echo "* Plik '$html' przetworzono do folderu '$folder'!\n";
}
}
@@ -69,9 +122,11 @@
$name = basename($file);;
$style = file_get_contents($file);
$style_file = fopen("public/css/" . $name, "w");
$style_file = fopen("$folder/css/" . $name, "w");
fwrite($style_file, $style);
fclose($style_file);
echo "* Plik '$css' przetworzono do folderu '$folder'!\n";
}
}
@@ -82,13 +137,91 @@
$name = basename($file);
$script = file_get_contents($file);
$script_file = fopen("public/js/" . $name, "w");
$script_file = fopen("$folder/js/" . $name, "w");
fwrite($script_file, $script);
fclose($script_file);
echo "* Plik '$js' przetworzono do folderu '$folder'!\n";
}
}
}
public function versionUp($type) : void{
switch($type){
case "pub":
$this->version++;
break;
case "dev":
$this->version += 0.1;
break;
}
$verfile = fopen("version", "w+");
fwrite($verfile, $this->version);
fclose($verfile);
}
public function addCustoms($type) : void {
$json = json_decode(file_get_contents("custom.json"), true);
switch($type){
case "dev":
foreach ($json["dev"]["css"] as $css){
$this->dictionary->page_tags["{CSS}"] .= "\n <link rel='stylesheet' href='$css' />";
echo "* Dodano '$css' do styli w '$type'!\n";
}
foreach ($json["dev"]["js"] as $js){
$this->dictionary->page_tags["{JS}"] .= "\n <script src='$js'></script>";
echo "* Dodano '$js' do skryptów w '$type'!\n";
}
break;
case "pub":
foreach ($json["pub"]["css"] as $css){
$this->dictionary->page_tags["{CSS}"] .= "\n <link rel='stylesheet' href='$css' />";
echo "* Dodano '$css' do styli w '$type'!\n";
}
foreach ($json["pub"]["js"] as $js){
$this->dictionary->page_tags["{JS}"] .= "\n <script src='$js'></script>";
echo "* Dodano '$js' do skryptów w '$type'!\n";
}
break;
}
}
public function runServer($type) : void{
if(file_exists("_config.php")){
$port = match ($type) {
"dev" => DEV_PORT,
default => PUB_PORT,
};
} else
$port = 14071;
echo "
=========================================
Uruchamianie serwera na adresie:
* http://127.0.0.1:$port *
Aby zatrzymać serwer, naciśnij Ctrl+C
=========================================
";
exec("php -S 127.0.0.1:$port -t " . __DIR__);
}
public function help() : void {
echo "
===============================
= BLOG ENGINE v0.1
= Krzysztof KrzysiekSiemv Smaga 2023
===============================
Dostępne komendy dla narzędzia:
* start-server [pub/dev] - uruchom serwer dla projektu. Opcja 'dev' specjalna dla programisty, opcja 'pub' dla każdego użytkownika. Domyślnie 'pub'
* help - wyświetl listę komend
* compile [pub/dev] - skompiluj projekt strony z folderu 'src'. Opcja 'dev' specjalna dla programisty, opcja 'pub' dla każdego użytkownika. Domyślnie 'pub'
";
}
}
$exec = new CompileIt();
$exec->main();
$exec->main($argv, $argc);