Skip to content

Commit

Permalink
Added new functions to the queries and refactored the code
Browse files Browse the repository at this point in the history
Changelog:

* First stable version :)
* Added get, pick, dump, dumpd, printr and printrd methods to the query results.
* Renamed the getTime method to getStartTime in the Session class.
* Renamed the 'for' tag to 'foreach' in the template system.
* Refactored code.
  • Loading branch information
Usbac committed Jun 5, 2019
1 parent f45da26 commit 33a1593
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 94 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
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.9-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-1.0.0-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 route, template, extensions and language system.
Expand Down Expand Up @@ -81,8 +81,6 @@ First time using it? Read the [Wiki](https://github.com/Usbac/Wolff/wiki).

## Contributing

Currently Wolff is quite stable but it's still a work in progress.

Any contribution or support to this project in the form of a pull request or message will be highly appreciated.

Don't be shy :)
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Home extends Controller
public function index()
{
$this->data['lang'] = $this->load->language('home');
$this->data['logo'] = getPublicDirectory() . 'logo.png';

$this->load->view('home', $this->data);
}

Expand Down
13 changes: 10 additions & 3 deletions app/views/home.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ CONFIG['title'] }} - Home</title>
<title>{{ getPageTitle() }} - Home</title>
<link href="{{ getPublicDirectory() }}assets/css/styles.css" rel="stylesheet"/>
<link rel="icon" href="{{ getPublicDirectory() }}logo.png">
<link rel="icon" href="{{ $logo }}">
</head>
<body>
<div class='center'>
{# Title #}
<div class='text-center'>
<img class='main-img' src="{{ getPublicDirectory() }}logo.png" width='130px'>
<img class='main-img' src="{{ $logo }}" width='130px'>
<h1 class='title'>{{ $lang['title'] }}</h1>
</div>
{# Description #}
<div class='text-center description'>
<p>{{ $lang['description'] }}</p>
<p>{{ $lang['description_2'] }}</p>
</div>
{# Options #}
<div class='text-center options'>
<a target='_blank' href='https://github.com/Usbac/Wolff/wiki'>{{ upper|$lang['documentation'] }}</a>
<a target='_blank' href='https://github.com/Usbac/Wolff'>{{ upper|$lang['github'] }}</a>
<a target='_blank' href='https://github.com/Usbac'>{{ upper|$lang['creators_page'] }}</a>
</div>
{# Version #}
<div class='text-center version'>
</i>{{ $lang['version'] }}</i>
</div>
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "usbac/wolff",
"description": "Ridiculously small PHP framework",
"keywords": [
"framework", "php", "small", "web", "fast", "mvc"
"framework", "php", "small", "web", "apps", "fast"
],
"type": "project",
"version": "0.9.9.9",
"version": "1.0.0",
"license": "MIT",
"support": {
"issues": "https://github.com/Usbac/Wolff/issues",
Expand Down
2 changes: 1 addition & 1 deletion public/assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* HOME */

body {
background-color: #F0F0F0;
background-color: #F5F5F5;
font-family: 'Open Sans', sans-serif;
font-size: 0.9em;
}
Expand Down
2 changes: 1 addition & 1 deletion system/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'public_dir' => $root . 'public/',

//General
'version' => '0.9.9.9',
'version' => '1.0.0',
'start' => microtime(true),
'title' => 'Wolff',
'main_page' => 'main_page',
Expand Down
5 changes: 3 additions & 2 deletions system/core/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public static function getSchema()
public static function getTableSchema(string $table)
{
$result = self::getPdo()->query("SHOW COLUMNS FROM $table");

if (is_bool($result)) {
return false;
}
Expand All @@ -276,7 +277,7 @@ public static function getTableSchema(string $table)
*/
public static function selectAll(string $table, string $conditions = '1', $args = null)
{
return DB::run("SELECT * FROM $table WHERE $conditions", $args)->rows;
return DB::run("SELECT * FROM $table WHERE $conditions", $args)->get();
}


Expand Down Expand Up @@ -307,7 +308,7 @@ public static function countAll(string $table, string $conditions = '1', $args =
*/
public static function deleteAll(string $table, string $conditions = '1', $args = null)
{
return DB::run("DELETE FROM $table WHERE $conditions", $args)->rows;
return DB::run("DELETE FROM $table WHERE $conditions", $args)->get();
}

}
64 changes: 3 additions & 61 deletions system/core/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ public static function session()
public static function loader()
{
if (!isset(self::$loader)) {
self::$loader = new Loader(self::template(),
self::session());
self::$loader = new Loader(self::template(), self::session());
}

return self::$loader;
Expand All @@ -236,68 +235,11 @@ public static function loader()
/**
* Returns a query result as an object
*
* @return stdClass a query result as an object
* @return Core\Query a query result as an object
*/
public static function query($results)
{
return new class($results)
{
/**
* The query result.
*
* @var array
*/
public $rows;


public function __construct($results)
{
$this->rows = $results;
}

/**
* Returns the query results as a Json
*
* @return string the query results as a Json
*/
public function toJson()
{
return json_encode($this->rows);
}

/**
* Returns the first element of the query results
*
* @return array the first element of the query results
*/
public function first()
{
return $this->rows[0] ?? null;
}

/**
* Returns the number of rows in the query results
*
* @return int the number of rows in the query results
*/
public function count()
{
return count($this->rows);
}

/**
* Returns the query result sliced
*
* @param int $start the offset
* @param int $end the length
*
* @return array the query result sliced
*/
public function limit(int $start, int $end)
{
return array_slice($this->rows, $start, $end);
}
};
return new Query($results);
}

}
133 changes: 133 additions & 0 deletions system/core/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Core;

class Query
{

/**
* The query result.
*
* @var array
*/
private $rows;


public function __construct($results)
{
$this->rows = $results;
}


/**
* Returns the query results
*
* @return array the query results
*/
public function get()
{
return $this->rows;
}


/**
* Returns the query results as a Json
*
* @return string the query results as a Json
*/
public function toJson()
{
return json_encode($this->rows);
}


/**
* Returns the first element of the query results
*
* @return array the first element of the query results
*/
public function first()
{
return $this->rows[0] ?? null;
}


/**
* Returns only the specified column of the query result
*
* @param string $column the column name to pick
*
* @return array only the specified column of the query result
*/
public function pick(string $column)
{
$rows = [];

foreach($this->rows as $row) {
$rows[] = array_key_exists($column, $row) ? $row[$column] : $row;
}

return $rows;
}


/**
* Returns the number of rows in the query results
*
* @return int the number of rows in the query results
*/
public function count()
{
return count($this->rows);
}


/**
* Returns the query result sliced
*
* @param int $start the offset
* @param int $end the length
*
* @return array the query result sliced
*/
public function limit(int $start, int $end)
{
return array_slice($this->rows, $start, $end);
}


/**
* Var dump the query results
*/
public function dump()
{
var_dump($this->rows);
}


/**
* Print the query results in a nice looking way
*/
public function printr()
{
printr($this->rows);
}


/**
* Var dump the query results and die
*/
public function dumpd()
{
dumpd($this->rows);
}


/**
* Print the query results in a nice looking way and die
*/
public function printrd()
{
printrd($this->rows);
}
}
26 changes: 13 additions & 13 deletions system/core/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,6 @@ public function count()
}


/**
* Add time to the session live time (in minutes)
*
* @param int $time the session live time to add
*/
public function addTime(int $time)
{
$_SESSION['end_time'] += $time * 60;
}


/**
* Get a live time (in minutes) of a session variable
*
Expand Down Expand Up @@ -203,7 +192,7 @@ public function getVarTime(string $key, bool $gmdate = false)
* Set a live time (in minutes) to a session variable
*
* @param string $key the variable key
* @param int $time the variable live time
* @param int $time the variable live time in minutes
*/
public function setVarTime(string $key, int $time = 1)
{
Expand All @@ -227,6 +216,17 @@ public function addVarTime(string $key, int $time = 1)
}


/**
* Add time to the session live time (in minutes)
*
* @param int $time the session live time to add
*/
public function addTime(int $time)
{
$_SESSION['end_time'] += $time * 60;
}


/**
* Set the session live time (in minutes) starting from
* the moment this function is called
Expand All @@ -247,7 +247,7 @@ public function setTime(int $time)
*
* @return mixed the session creation time
*/
public function getTime(bool $gmdate = false)
public function getStartTime(bool $gmdate = false)
{
if ($gmdate) {
return gmdate(self::DATE_FORMAT, $_SESSION['start_time']);
Expand Down
Loading

0 comments on commit 33a1593

Please sign in to comment.