Skip to content

Commit

Permalink
Added a Logger class and minor changes
Browse files Browse the repository at this point in the history
Changelog:

* Added a Logger class.
* Renamed the create/make folder functions from the Cache and Extension class to mkdir.
* Added getSystemDirectory function to the standard library.
* Fixed bug when loading a non-existent view.
* Added get function to the Cache class.
* Refactored code.
  • Loading branch information
Usbac committed May 22, 2019
1 parent aaa74e5 commit 16508ff
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
/vendor/
/cache/
/system/logs/
/app/extensions/
composer.lock
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@
<h4 align="center">Ridiculously small PHP framework.</h4>

<p align="center">
<img src="https://img.shields.io/badge/stability-stable-green.svg"> <img src="https://img.shields.io/badge/version-0.9.9.3-blue.svg"> <img src="https://img.shields.io/badge/license-MIT-orange.svg">
<img src="https://img.shields.io/badge/stability-stable-green.svg"> <img src="https://img.shields.io/badge/version-0.9.9.4-blue.svg"> <img src="https://img.shields.io/badge/license-MIT-orange.svg">
</p>

Wolff is a ridiculously small and lightweight PHP framework with useful functions and utilities like a template, route, extensions and language system.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -10,7 +10,7 @@
"mvc"
],
"type": "project",
"version": "0.9.9.3",
"version": "0.9.9.4",
"license": "MIT",
"support": {
"issues": "https://github.com/Usbac/Wolff/issues",
Expand Down
25 changes: 17 additions & 8 deletions system/cli/Create.php
Expand Up @@ -3,6 +3,7 @@
namespace Cli;

use Core\{Extension, Maintenance};
use Utilities\Str;

class Create
{
Expand Down Expand Up @@ -92,10 +93,13 @@ private function controller()

$file = fopen($file_dir, 'w') or die("WARNING: Cannot create Controller file \n");

$values = [
'namespace' => $namespace,
'classname' => $file_name
];

$content = file_get_contents(self::TEMPLATE_PATH . 'controller.txt');
$original = array('{namespace}', '{classname}');
$replacement = array($namespace, $file_name);
$content = str_replace($original, $replacement, $content);
$content = Str::interpolate($content, $values);

fwrite($file, $content);
fclose($file);
Expand Down Expand Up @@ -130,7 +134,7 @@ private function view()
$file = fopen($file_dir, 'w') or die("WARNING: Cannot create view file \n");

$content = file_get_contents(self::TEMPLATE_PATH . 'view.txt');
$content = str_replace('{title}', $this->argv[3], $content);
$content = Str::interpolate($content, ['title' => $this->argv[3]]);

fwrite($file, $content);
fclose($file);
Expand All @@ -148,7 +152,7 @@ private function extension()
}

$file_dir = getExtensionDirectory() . $this->argv[3] . '.php';
Extension::makeFolder();
Extension::mkdir();

if (file_exists($file_dir)) {
echo "\e[1;31m WARNING: extension " . $this->argv[3] . " already exists!\e[0m \n";
Expand All @@ -165,11 +169,16 @@ private function extension()
$version = readline("Version -> ");
$author = readline("Author -> ");

$original = array('{classname}', '{name}', '{description}', '{version}', '{author}');
$replacement = array($this->argv[3], $name, $description, $version, $author);
$values = [
'classname' => $this->argv[3],
'name' => $name,
'description' => $description,
'version' => $version,
'author' => $author,
];

$content = file_get_contents(self::TEMPLATE_PATH . 'extension.txt');
$content = str_replace($original, $replacement, $content);
$content = Str::interpolate($content, $values);

fwrite($file, $content);
fclose($file);
Expand Down
3 changes: 2 additions & 1 deletion system/config.php
Expand Up @@ -11,13 +11,14 @@

//Directories
define('WOLFF_ROOT_DIR', dirname(__DIR__) . '/');
define('WOLFF_SYS_DIR', WOLFF_ROOT_DIR . 'system/');
define('WOLFF_APP_DIR', WOLFF_ROOT_DIR . 'app/');
define('WOLFF_PUBLIC_DIR', WOLFF_ROOT_DIR . 'public/');
define('WOLFF_EXTENSION_DIR', WOLFF_APP_DIR . 'extensions/');
define('WOLFF_CACHE_DIR', WOLFF_ROOT_DIR . 'cache/');

//General
define('WOLFF_VERSION', '0.9.9.3');
define('WOLFF_VERSION', '0.9.9.4');
define('WOLFF_START', microtime(true));
define('WOLFF_PAGE_TITLE', 'Wolff');
define('WOLFF_MAIN_PAGE', 'main_page');
Expand Down
64 changes: 40 additions & 24 deletions system/core/Cache.php
Expand Up @@ -39,19 +39,32 @@ public static function isEnabled()
}


/**
* Return the specified cache file path
*
* @param string $dir the cache filename
*
* @return string return the specified cache file path
*/
public static function get(string $dir)
{
return getCacheDirectory() . self::getFilename($dir);
}


/**
* Create the cache file if doesn't exists and return its path
*
* @param string $dir the view directory
* @param string $dir the cache filename
* @param string $content the original file content
*
* @return string the cache file path
*/
public static function get(string $dir, string $content)
public static function set(string $dir, string $content)
{
$file_path = getCacheDirectory() . self::getFilename($dir);
$file_path = self::get($dir);

self::createFolder();
self::mkdir();

if (!file_exists($file_path)) {
$file = fopen($file_path, 'w');
Expand Down Expand Up @@ -83,7 +96,7 @@ public static function expired($dir)
/**
* Create the cache folder if it doesn't exists
*/
public static function createFolder()
public static function mkdir()
{
$folder_path = getCacheDirectory();
if (!file_exists($folder_path)) {
Expand All @@ -99,40 +112,43 @@ public static function createFolder()
*
* @return bool true if the cache exists, false otherwise
*/
public static function exists(string $dir = '')
public static function has(string $dir)
{
$folder_path = getCacheDirectory();

if (!empty($dir)) {
$file_path = $folder_path . self::getFilename($dir);

return is_file($file_path);
}
$file_path = getCacheDirectory() . self::getFilename($dir);

return !empty(glob($folder_path . '*'));
return is_file($file_path);
}


/**
* Delete all the cache files or the specified one
* Delete the specified cache file
*
* @param string $dir the cache to delete, if its empty all the cache will be deleted
*
* @return bool true if the item was successfully removed, false otherwise
*/
public static function clear(string $dir = '')
public static function delete(string $dir)
{
$folder_path = getCacheDirectory();
$file_path = getCacheDirectory() . self::getFilename($dir);

if (empty($dir)) {
deleteFilesInDir($folder_path);
if (is_file($file_path)) {
unlink($file_path);

return;
return true;
}

$file_path = $folder_path . self::getFilename($dir);
return false;
}

if (is_file($file_path)) {
unlink($file_path);
}

/**
* Delete all the cache files
*
* @return bool true if the item was successfully removed, false otherwise
*/
public static function clear()
{
return deleteFilesInDir(getCacheDirectory());
}


Expand Down
2 changes: 1 addition & 1 deletion system/core/DB.php
Expand Up @@ -53,7 +53,7 @@ public function __construct()
self::$connection = new PDO(strtolower(getDBMS()) . ':host=' . getServer() . '; dbname=' . getDB() . '',
getDBUser(), getDBPass(), array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
} catch (PDOException $e) {
error_log($e->getMessage());
Log::critical($e->getMessage());
}
}

Expand Down
4 changes: 2 additions & 2 deletions system/core/Extension.php
Expand Up @@ -35,7 +35,7 @@ public static function load(string $type, $loader = null)
return false;
}

self::makeFolder();
self::mkdir();

foreach (self::$extensions as $extension) {
if ($extension['type'] !== $type || !self::matchesRoute($extension['route'])) {
Expand Down Expand Up @@ -125,7 +125,7 @@ public static function folderExists()
/**
* Make the extension folder directory if it doesn't exists
*/
public static function makeFolder()
public static function mkdir()
{
if (!self::folderExists()) {
mkdir(getExtensionDirectory());
Expand Down
4 changes: 2 additions & 2 deletions system/core/Factory.php
Expand Up @@ -28,7 +28,7 @@ public static function controller(string $dir = null)
$class = self::NAMESPACE_CONTROLLER . Str::pathToNamespace($dir);

if (!class_exists($class)) {
error_log("Warning: The controller class '" . $class . "' doesn't exists");
Log::error("The controller class '$class' doesn't exists");

return false;
}
Expand All @@ -49,7 +49,7 @@ public static function extension(string $name)
$class = self::NAMESPACE_EXTENSION . $name;

if (!class_exists($class)) {
error_log("Warning: The Extension class '" . $class . "' doesn't exists");
Log::error("The extension class '$class' doesn't exists");

return false;
}
Expand Down
24 changes: 23 additions & 1 deletion system/core/Loader.php
Expand Up @@ -69,6 +69,7 @@ public function getUpload()
*/
public function controller(string $dir)
{
$controller_path = $dir;
$dir = Str::sanitizePath($dir);

//load controller default function and return it
Expand Down Expand Up @@ -104,6 +105,8 @@ public function controller(string $dir)
return $controller;
}

Log::error("Controller '$controller_path' doesn't exists");

return false;
}

Expand Down Expand Up @@ -152,10 +155,14 @@ public function language(string $dir, string $language = WOLFF_LANGUAGE)

if (file_exists($file_path)) {
include_once($file_path);
} else {
Log::warning("The $language language for '$dir' doesn't exists");

return false;
}

if (!isset($data)) {
error_log("Warning: The " . $language . " language for '" . $dir . "' doesn't exists");
Log::warning("The $language language content for '$dir' is empty");

return false;
}
Expand All @@ -174,6 +181,14 @@ public function language(string $dir, string $language = WOLFF_LANGUAGE)
public function view(string $dir, array $data = [], bool $cache = true)
{
$dir = Str::sanitizePath($dir);
$file_path = getAppDirectory() . 'views/' . $dir;

if (!file_exists($file_path . '.php') && !file_exists($file_path . '.html')) {
Log::error("View '$dir' doesn't exists");

return;
}

$this->template->get($dir, $data, $cache);
}

Expand All @@ -189,6 +204,13 @@ public function view(string $dir, array $data = [], bool $cache = true)
public function getView(string $dir, array $data = [])
{
$dir = Str::sanitizePath($dir);
$file_path = getAppDirectory() . 'views/' . $dir;

if (!file_exists($file_path . '.php') && !file_exists($file_path . '.html')) {
Log::error("View '$dir' doesn't exists");

return;
}

return $this->template->getView($dir, $data);
}
Expand Down

0 comments on commit 16508ff

Please sign in to comment.