2021-04-14 21:48:57 +02:00
|
|
|
<?php
|
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
namespace brain\utility;
|
|
|
|
|
2021-04-14 21:48:57 +02:00
|
|
|
class DocTools
|
|
|
|
{
|
2022-03-14 01:46:42 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2021-04-14 21:48:57 +02:00
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public static function writePages($task, $path, $fileLocation, $fileContents)
|
|
|
|
{
|
|
|
|
try {
|
2022-05-17 02:41:15 +02:00
|
|
|
if ($task == 'create') {
|
|
|
|
if (!is_dir('../content/pages/' . $path)) {
|
2022-03-19 00:00:51 +01:00
|
|
|
//Directory does not exist, so lets create it.
|
2022-05-17 02:41:15 +02:00
|
|
|
mkdir('../content/pages/' . $path, 0755, true);
|
2022-03-14 01:46:42 +01:00
|
|
|
}
|
|
|
|
file_put_contents($fileLocation, $fileContents);
|
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
($new = fopen($fileLocation, 'w')) or die('Unable to open file!');
|
2022-03-14 01:46:42 +01:00
|
|
|
fwrite($new, $fileContents);
|
|
|
|
fclose($new);
|
|
|
|
}
|
2021-04-18 02:32:45 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
return true;
|
|
|
|
} catch (Error $error) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-23 01:04:43 +02:00
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public static function writeSettings($fileLocation, $fileContents)
|
|
|
|
{
|
|
|
|
if (!is_file($fileLocation)) {
|
|
|
|
file_put_contents($fileLocation, json_encode($fileContents));
|
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
($new = fopen($fileLocation, 'w')) or die('Unable to open file!');
|
2022-03-14 01:46:42 +01:00
|
|
|
fwrite($new, json_encode($fileContents));
|
|
|
|
fclose($new);
|
|
|
|
}
|
2021-04-23 01:04:43 +02:00
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public static function writeHTML($location, $html, $path = null)
|
|
|
|
{
|
|
|
|
if ($path != null) {
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
//Directory does not exist, so lets create it.
|
|
|
|
mkdir($path, 0755, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_file($location)) {
|
|
|
|
file_put_contents($location, $html);
|
2021-04-25 01:37:25 +02:00
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
($new = fopen($location, 'w')) or die('Unable to open file!');
|
2022-03-14 01:46:42 +01:00
|
|
|
fwrite($new, $html);
|
|
|
|
fclose($new);
|
2021-04-25 01:37:25 +02:00
|
|
|
}
|
2022-03-14 01:46:42 +01:00
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public static function deleteFolder($path)
|
|
|
|
{
|
|
|
|
if (!empty($path) && is_dir($path)) {
|
2022-03-20 00:15:56 +01:00
|
|
|
$dir = new \RecursiveDirectoryIterator(
|
2022-03-14 01:46:42 +01:00
|
|
|
$path,
|
2022-03-20 00:15:56 +01:00
|
|
|
\RecursiveDirectoryIterator::SKIP_DOTS
|
2022-03-14 01:46:42 +01:00
|
|
|
); //upper dirs are not included,otherwise DISASTER HAPPENS :)
|
2022-03-20 00:24:19 +01:00
|
|
|
$files = new \RecursiveIteratorIterator(
|
2022-03-14 01:46:42 +01:00
|
|
|
$dir,
|
2022-03-20 00:15:56 +01:00
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST
|
2022-03-14 01:46:42 +01:00
|
|
|
);
|
|
|
|
foreach ($files as $f) {
|
|
|
|
if (is_file($f)) {
|
|
|
|
unlink($f);
|
|
|
|
} else {
|
|
|
|
$empty_dirs[] = $f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($empty_dirs)) {
|
|
|
|
foreach ($empty_dirs as $eachDir) {
|
|
|
|
rmdir($eachDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir($path);
|
2021-04-25 01:37:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public static function objectToMD($object)
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$markdown = "---\n" .
|
|
|
|
'id: ' .
|
|
|
|
$object['id'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'uuid: ' .
|
|
|
|
$object['uuid'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'title: ' .
|
2022-03-14 01:46:42 +01:00
|
|
|
"'" .
|
2022-05-17 02:41:15 +02:00
|
|
|
$object['title'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"'" .
|
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'feature: ' .
|
|
|
|
$object['feature'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'files: ' .
|
|
|
|
$object['files'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'path: ' .
|
|
|
|
$object['path'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'layout: ' .
|
|
|
|
$object['layout'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'tags: ' .
|
|
|
|
$object['tags'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'author: ' .
|
|
|
|
$object['author'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'created: ' .
|
|
|
|
$object['created'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'updated: ' .
|
|
|
|
$object['updated'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'deleted: ' .
|
|
|
|
$object['deleted'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'slug: ' .
|
|
|
|
$object['slug'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'menu: ' .
|
|
|
|
$object['menu'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'published: ' .
|
|
|
|
$object['published'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
'featured: ' .
|
|
|
|
$object['featured'] .
|
2022-03-14 01:46:42 +01:00
|
|
|
"\n---\n" .
|
2022-05-17 02:41:15 +02:00
|
|
|
$object['content'];
|
2021-04-18 02:32:45 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
return $markdown;
|
|
|
|
}
|
2021-04-14 21:48:57 +02:00
|
|
|
}
|