Skip to content

Commit

Permalink
Merge pull request #10 from skipperbent/feature-cleanup
Browse files Browse the repository at this point in the history
[CLEANUP] Cleaned up
  • Loading branch information
skipperbent committed Oct 22, 2015
2 parents 42093c0 + d9c7360 commit d268e79
Show file tree
Hide file tree
Showing 31 changed files with 274 additions and 370 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
# [WIP] pecee php framework

Fast, lightweight, open source MVC PHP framework based on Microsoft MVVM.
**Fast, lightweight, open source MVC PHP framework based on Microsoft MVVM.**

Features:
## Demo project

- Fully customisable template engine. Overwrite render() method to add your own templating engine or create your own using the Taglib class.
Demo project available here: https://github.com/skipperbent/pecee-project

- Every template has class, called a Widget, behind it. From here you can set properties, create methods and you can even render widgets inside a widget - this makes it super easy the create small pieces of functionality, that can be reused wherever you like.
## Features:

- Because every template is basiclly a class, you can extend functionality from other widgets and reuse the same functionality or overwrite the things you want to change.
- Fully customisable template engine. Overwrite render() method to add your own templating engine through composer or create your own.

- Don't like something? Everything is 100% object oriented, so every little piece of code can be easily extended. A great example is PeceeCamp which uses some of the functionality of PeceeCamp, but completely rewrites the way routing is working.
- Every template has class, called a Widget, behind which renders before the view. From here you can set properties, create methods and you can even render widgets inside a widget - this makes it super easy the create small pieces of functionality, that can be reused wherever you like.

## Installation ##
- Because every template is basically a class, you can extend functionality from other widgets and reuse the same functionality or overwrite the things you want to change.

- Don't like something? Everything is 100% object oriented, so every little piece of code can be easily extended.

## Installation

```
cd /path/to/project
git clone https://sessingo@bitbucket.org/sessingo/pecee-project.git
composer install
```

Point your webserver to /path/to/project/app/www
Point your webserver to ```/path/to/project/app/www```

We recommend that you look at the demo project for futher installation instructions:
https://github.com/skipperbent/pecee-project

## The MIT License (MIT)

Expand Down
6 changes: 5 additions & 1 deletion config/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function response() {
return new \Pecee\Http\Response();
}

function request() {
return new \Pecee\Http\Request();
}

function redirect($url, $code = null) {
$response = new \Pecee\Http\Response();

Expand All @@ -28,7 +32,7 @@ function lang($key, $args = null) {
$args = func_get_args();
$args = array_slice($args, 1);
}
return \Pecee\Language::getInstance()->_($key, $args);
return \Pecee\Translation::getInstance()->_($key, $args);
}

/**
Expand Down
28 changes: 8 additions & 20 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class Collection {
protected $data;

public function __construct(array $rows = null) {
if(!is_null($rows)) {
if($rows !== null) {
$this->setData($rows);
}
}

public function setData(array $arr) {
$this->data=array();
foreach($arr as $i=>$row) {
$this->__set(strtolower($i), $row);
$this->data = array();
foreach($arr as $key => $value) {
$this->__set($key, $value);
}
}

Expand All @@ -32,24 +32,12 @@ public function __set($name, $value) {
$this->data[strtolower($name)] = $value;
}

public function __call($name, $args) {
if(strlen($name) > 3) {
switch(substr(strtolower($name), 0, 3)) {
case 'get':
return $this->__get(substr($name, 3));
break;
case 'set':
$this->__set(substr($name, 3), $args[0]);
return null;
break;
}
}

throw new \InvalidArgumentException('The field "'.$name.'" couldt not be found');
}

public function getData() {
return $this->data;
}

public function add($key, $value) {
$this->data[$key] = $value;
}

}
42 changes: 2 additions & 40 deletions src/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,8 @@ public function __construct() {
parent::__construct();
}

public function hasParam($param) {
return (isset($_GET[$param]));
public function asJson(array $array) {
return request()->cache()->json($array);
}

public function getPost($index,$default=null) {
if(isset($_POST[$index])) {
return $_POST[$index];
}
foreach($_POST as $key=>$post) {
if(strpos($key, '_') > -1) {
$key = explode('_',$key);
if($key[1] == $index) {
return $post;
}
}
}
return $default;
}

public function asJSON(array $array, $cacheDuration = 2592000) {
if(!is_null($cacheDuration)) {
header('Cache-Control: public,max-age='.$cacheDuration.',must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s',(time()+$cacheDuration)).' GMT');
header('Last-modified: '.gmdate('D, d M Y H:i:s',time()).' GMT');
}
header('Content-type: application/json');
echo json_encode($array);
die();
}

/**
* @param string $name
* @param array|null $args
*
* Simular to PHP __call() - called whenever a method is invoked from the router
*/
public function callAction($name, $args = null) {
}

public function destruct() {
$this->__destruct();
}
}
2 changes: 2 additions & 0 deletions src/Controller/ControllerCss.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use Pecee\Controller\File\FileAbstract;

class ControllerCss extends FileAbstract {

public function __construct() {
parent::__construct(FileAbstract::TYPE_CSS);
}

}
3 changes: 3 additions & 0 deletions src/Controller/ControllerJs.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
namespace Pecee\Controller;

use Pecee\Controller\File\FileAbstract;

class ControllerJs extends FileAbstract {

public function __construct() {
parent::__construct(FileAbstract::TYPE_JAVASCRIPT);
}

}
27 changes: 19 additions & 8 deletions src/Controller/File/FileAbstract.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
namespace Pecee\Controller\File;

use Pecee\Controller\Controller;
use Pecee\Registry;
use Pecee\Str;
use Pecee\UI\Site;

abstract class FileAbstract extends Controller {

protected $files;
protected $cacheDate;
protected $type;
Expand All @@ -22,25 +24,29 @@ public function __construct($type) {
if(!in_array($type, self::$types)) {
throw new \InvalidArgumentException(sprintf('Unknown type, must be one of the following: %s', join(', ', self::$types)));
}
$this->type = $type;

$this->type = $type;
$this->tmpDir = dirname($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR . 'cache';
}

public function getWrap($files = null) {
// Set time limit
set_time_limit(60);

$this->files = $files;
$this->cacheDate = $this->getParam('_', '');
header('Content-type: '.$this->getHeader());
header('Charset: ' . \Pecee\UI\Site::getInstance()->getCharset());
header('Cache-Control: must-revalidate');
header('Expires: ' . gmdate("D, d M Y H:i:s", time() + 9600) . ' GMT');

// Set headers
request()->cache()->headers([
'Content-type: '.$this->getHeader(),
'Charset: ' . Site::getInstance()->getCharset(),
]);

if(!in_array('ob_gzhandler', ob_list_handlers())) {
ob_start ("ob_gzhandler");
}

if(isset($_GET['__clearcache']) && Site::getInstance()->hasAdminIp() && is_dir($this->tmpDir)) {
if($this->hasParam('__clearcache') && Site::getInstance()->hasAdminIp() && is_dir($this->tmpDir)) {
$handle = opendir($this->tmpDir);
while (false !== ($file = readdir($handle))) {
if($file == (md5($this->files . $this->cacheDate) . '.' . $this->type)) {
Expand All @@ -53,8 +59,10 @@ public function getWrap($files = null) {
if(!file_exists($this->getTempFile()) || Registry::getInstance()->get('DisableFileWrapperCache', false)) {
$this->saveTempFile();
}

echo file_get_contents($this->getTempFile(), FILE_USE_INCLUDE_PATH);
}

protected function saveTempFile() {
if($this->files) {
$files = (strpos($this->files, ',')) ? @explode(',', $this->files) : array($this->files);
Expand Down Expand Up @@ -119,6 +127,7 @@ protected function saveTempFile() {
protected function debugMode() {
return (strtolower($this->getParam('__debug')) == 'true' && Site::getInstance()->hasAdminIp());
}

protected function getHeader() {
switch($this->type) {
case self::TYPE_CSS:
Expand All @@ -130,17 +139,19 @@ protected function getHeader() {
}
return '';
}

protected function getPath() {
switch($this->type) {
case self::TYPE_JAVASCRIPT:
return \Pecee\UI\Site::getInstance()->getJsPath();
return Site::getInstance()->getJsPath();
break;
case self::TYPE_CSS:
return \Pecee\UI\Site::getInstance()->getCssPath();
return Site::getInstance()->getCssPath();
break;
}
return '';
}

protected function getTempFile() {
return sprintf('%s.%s', $this->tmpDir.DIRECTORY_SEPARATOR.md5($this->files), $this->type);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static function create($id, $value, $time = null, $domain = null, $secure
$sub = explode('.',$_SERVER['HTTP_HOST']);
$domain = (count($sub) > 2) ? $_SERVER['HTTP_HOST'] : '.' . $_SERVER['HTTP_HOST'];
}
return (@setCookie($id, $value, (($time > 0) ? $time : null), '/', $domain, $secure));
return (@setcookie($id, $value, (($time > 0) ? $time : null), '/', $domain, $secure));
}

public static function get($id,$defaultValue = null) {
Expand Down
5 changes: 3 additions & 2 deletions src/Dataset/Dataset.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Pecee\Dataset;
abstract class Dataset {

protected $data = array();

/**
Expand All @@ -12,7 +13,7 @@ public function getData() {

public function get($index) {
for($i = 0; $i < count($this->data); $i++) {
if($i == $index) {
if($i === $index) {
return $this->data[$i];
}
}
Expand All @@ -29,7 +30,7 @@ public function setData($data) {

protected function add($value=null, $name) {
$arr = array();
if(!is_null($value)) {
if($value !== null) {
$arr['value'] = htmlspecialchars($value);
}
$arr['name'] = $name;
Expand Down
3 changes: 2 additions & 1 deletion src/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Pecee\Widget\Debug\WidgetDebug;

class Debug {
private static $instance;

protected static $instance;
protected $enabled;
protected $lastTime;
protected $stack;
Expand Down
14 changes: 7 additions & 7 deletions src/Guid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
namespace Pecee;
class Guid {

public static function create($seperator = false) {
public static function create($separator = false) {
if(function_exists('com_create_guid')) {
$guid= trim(com_create_guid(), '{}');
return (!$seperator) ? str_replace('-', '', $guid) : $guid;
$guid = trim(com_create_guid(), '{}');
return (!$separator) ? str_replace('-', '', $guid) : $guid;
}
$pattern=(!$seperator) ? '%04X%04X%04X%04X%04X%04X%04X%04X' : '%04X%04X-%04X-%04X-%04X-%04X%04X%04X';
return sprintf($pattern,
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535),
mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535),
$pattern = (!$separator) ? '%04X%04X%04X%04X%04X%04X%04X%04X' : '%04X%04X-%04X-%04X-%04X-%04X%04X%04X';
return sprintf($pattern,
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535),
mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535),
mt_rand(0, 65535), mt_rand(0, 65535));
}

Expand Down
10 changes: 5 additions & 5 deletions src/IO/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public static function getExtensionByMimeType($mimeType) {
return 'png';
break;
}
throw new \InvalidArgumentException('Unknown image!');
throw new \InvalidArgumentException('Unsupported image type');
}

public static function getImageType($filePath) {
switch (File::GetExtension($filePath)) {
switch (File::getExtension($filePath)) {
case 'jpeg':
case 'jpg':
return IMAGETYPE_JPEG;
Expand All @@ -53,7 +53,7 @@ public static function getImageType($filePath) {
return IMAGETYPE_PNG;
break;
default:
throw new \ErrorException('Ukendt filtype: ' . File::GetExtension($filePath));
throw new \ErrorException('Unknown file extension: ' . File::getExtension($filePath));
}
}

Expand Down Expand Up @@ -94,7 +94,7 @@ protected function getImage($image) {
imagepng($image, null, min($this->quality,9));
break;
default:
throw new \ErrorException('Ukendt mimetype: ' . $this->mimeType);
throw new \ErrorException('Unknown mimetype: ' . $this->mimeType);
}
$fileContents = ob_get_contents();
ob_end_clean();
Expand All @@ -121,7 +121,7 @@ public function getWithWatermark($watermarkPath) {
public function getResizedExact($dstWidth,$dstHeight) {
$src = @imagecreatefromstring($this->originalBinary);
if (!$src)
throw new \ErrorException('Kunne ikke læse fil.');
throw new \ErrorException('Failed to read file');
$srcWidth = imagesx($src);
$srcHeight = imagesy($src);
$widthRatio = $srcWidth / $dstWidth;
Expand Down
Loading

0 comments on commit d268e79

Please sign in to comment.