2017 © Pedro Peláez
 

library pie

A light weight router, memcache + SQL handling, and session management library.

image

pie/pie

A light weight router, memcache + SQL handling, and session management library.

  • Friday, January 8, 2016
  • by voltrue2
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

pie Library

A very light weight PHP library to handle routing, controller management, SQL + memcache, and logging., (*1)

How To Use

Require pie Library

require_once('pie/index.php');

Create Bootstrap

In your index.php file, you will require something similar to the following:, (*2)

require_once('pie/index.php');

// set up loader
Loader::setRootPath('/path/to/my/application/');

// set up configurations
Config::set('console.filePath', '/path/to/my/application/logs/server.' . date('Ymd') . '.log');
Config::set('console.noClient', false);
Config::set('console.verbose', false);
Config::set('controllerPath', '/path/to/my/application/controller/');

// set up logger
Console::setup(Config::get('console.filePath'), Config::get('console.noClient'), Config::get('console.verbose'));

// set up and run router
$router = new Router();
$router->setTrailingSlash(true);
$router->setControllerPath(Config::get('controllerPath'));

// set URI prefix: The URI parser will assume that every URI starts with the given prefix
$router->setUriPrefix('mobile');

// add reroute
$router->addReroute('/', '/test/index');

// error handling reroute
$router->addErrorReroute(404, '/error/notfound');

// start the app
$router->run();

Classes

These classes will be included when you you pie library., (*3)

Loader

A static class to handle include., (*4)

Static Methods

::setRootPath($path [string])

Set a root path for Loader to use as the root path., (*5)

::get($path)

Static method to load a PHP source code., (*6)

Example:, (*7)

Loader::get('my/awesome/php/lib.php');
::getRootPath()

Static method to return the root path for Loader., (*8)

Config

A static class to handle setting and getting configuration values across the application., (*9)

Static Methods

::set($name [string], $value [mixed])
::get($name [string])

Returns the value of configuration by its name set by .set()., (*10)

Router

A router class to handle routing., (*11)

Methods

::redirect($uri [string], $statusCode [*number])

A static method to redirect., (*12)

Example:, (*13)

Router::redirect('/redirect/here/', 301);
->setTrailingSlash($enable [boolean])

Enable/Disable enforced trailing slash in URL., (*14)

->setControllerPath($path [string])

Set controller directory path where all controllers are., (*15)

->addReroute($from [string], $to [$to])

Adds a rerouting path., (*16)

Exmaple:, (*17)

$router->addReroute('/', '/reroute/here/');

The above example will reroute / to /reroute/here/., (*18)

->addErrorReroute($code [number], $controllerName)

Assign a URI to a specific error code such as 404., (*19)

$router->addErrorReroute(404, '/error/notfound/');

The above example will execute /error/notfound/ on every 404 error., (*20)

->addRequestHook($uri [string], $funcName [string], $class [*mixed])

Registers a callback function on specified URI given by $uri., (*21)

The registered callback will be called on every matching request., (*22)

NOTE 1: The callback MUST return HTTP status code for an error., (*23)

If there is no error in the hook, you may return 200 or execute some functions., (*24)

Example:, (*25)

$router->addReuqestHook('/test/index/', 'myMethod', 'myRequestHookHandlerClass');

class myRequestHookHandlerClass {

    public static function myMethod($request, $response) {
        // check session
        if (/* no session */) {
            return 403;
        }
        // there is a session
        $response->redirect('/mypage/');
    }

}

NOTE 2: The hooks that are added to controller ONLY will be executed for all requests with the same controller., (*26)

Example:, (*27)

$router->addRequestHook('/example/', 'myHook');
// the above will be exected on:
/*
    /example/
    /example/index/
    /example/boo/
    /example/foo/ 
    etc...
*/

Console

A static class for logging both on server and client (browser)., (*28)

Static Methods

::setup($filePath [string], $noClient [boolean], $verbose [*boolean])

Set up Console class., (*29)

$filePath

If given, Console will be logging to the path given on the server., (*30)

$noClient

If false, Console will not be logging in console of browser., (*31)

Default value is false., (*32)

$verbose

If false Console will not output log, but warn and error only., (*33)

Default value is true., (*34)

::create($name [*string])

Returns an instance of ConsoleCore object for logging., (*35)

Example:, (*36)

Console::init('/logging/path/', true);
$console = Console::create();

$console->log('boo');
$console->warn('foo');
$console->error('too');

NOTE: Console will catch uncaught exceptions and log the error automatically, (*37)

Controller

pie library handles each request by a cooresponding controller., (*38)

For example a URL /example/index will be executing a controller class in controller/example/index.class.php., (*39)

How To Create A Controller Class

First, you must set a controller path as shown below:, (*40)

$router = new Router();
$router->setControllerPath('path/to/my/controller/');

You will then define a URI by creating a controller directory and a method file in it:, (*41)

# Define controller 'example'
mkdir path/to/my/controller/example
# A controller method called index
path/to/my/controller/example/index.class.php

Controller Class

A controller must be a valid PHP class such as:, (*42)

getData($dataName [string])

Returns a matching request data of GET/POST/PUT/DELETE

###### ->getAllData()

Returns a map of all request data of GET/POST/PUT/DELETE

###### ->getHeader($name [string])

Returns a matching request header.

###### ->getAllHeaders()

Returns all request headers.

##### $response

###### ->assign($name [string], $value [mixed])

Assigns variables to be used in response output.

###### ->html($source [string], $statusCode [*number])

If `$source` is a path to a template file, it will load it and output its content.

If `$source` is a string value, it will output as it is.

###### ->json($statusCode [*number])

Outputs assigned variables (by .assign()) as a JSON string.

###### ->redirect($uri [string], $statusCode [*number])

Redirects to the given `$uri` with given `status code`.

If `status code` is not given, it will default to '301'.

##### $params

An array of URL parameters.

Examaple:

```
/example/index/one/two/three

$params = array(
    'one',
    'two',
    'three'
);
```

## Data

A static class to output "assigned" values on controllers to templates.

#### Static Methods

#### ::get($assignedName [string])

Returns a value assigned by `$response->assign()`.

Example:

```php
// in your controller
$response->assign('boo', 'Boo');
```

```html

= Data::get('boo') ?>

DataSource

pie library comes with a very simple SQL + Memcache class., (*43)

How To Setup DataSource

We need to properly setup DataSource., (*44)

NOTE: DataSource supports both mysql and pgsql., (*45)

// this creates a data model
DataSource::create('myModel');
// this is how you access the created data model anywhere in your application
$model = DataSource::get('myModel');
// ttl of cache to be 60000ms
$model->setupCache('localhost', 11211, 60000);
$model->setupMaster('mysql', 'localhost', 'myDBName', 'myUser', 'myPassword');
// typically slave would have different configurations than master
$model->setupSlave('mysql', 'localhost', 'myDBName', 'myUser', 'myPassword');

Handling SQL

With DataSource, we have data model., (*46)

$myModel = DataSource::get('myModel');

DataSource Class

Static Methods

::create(dataModelName [string])

Create a new instance of a data model to access SQL., (*47)

DataSource::create('myDataModel');
::get(dataModelName [string])

Return an instance of a data model created by ::create()., (*48)

DataSource::create('myDataModel');
$model = DataSource::get('myDataModel');

Model Class

This class is to handle SQL queries and memchache., (*49)

Methods

->setupCache($host [string], $port [number], $ttl [number])

Setup memcache connection and TTL (it is in seconds)., (*50)

->ignoreCache()

Disable memcache., (*51)

->setupMaster($type [string], $host [string], $dbName [string], $user [string], $password [string])

Setup SQL master connection., (*52)

$type = 'mysql' or 'pqsql'
->setupSlave($type [string], $host [string], $dbName [string], $user [string], $password [string])

Setup SQL slave connection., (*53)

$type = 'mysql' or 'pqsql'
->read($sql [string], $params [*array])

Returns the results of the query., (*54)

This method reads from slave., (*55)

Memcache is used., (*56)

->readForWrite($sql [string], $params [*array]);

Returns the results of the query., (*57)

This method reads from master., (*58)

Memcache is NOT used., (*59)

->write($sql [string], $params [*array])

Executes the query on master with auto-rollback on an exception error if in transaction., (*60)

Updates memcache time., (*61)

->transaction()

Starts a transaction., (*62)

->commit()

Commits transactioned queries., (*63)

->rollback()

Rolls back transactioned queries., (*64)

Uid Class

A static class to create a unique ID., (*65)

The ID is a string., (*66)

$uid = Uid::create();

Static Methods

::create()

Returns a unique ID string., (*67)

Encrypt Class

A static class to create a hash and validates., (*68)

Useful for secure password validation., (*69)

$pass = 'secret';
$hash = Encrypt::createHash($pass);
$validated = Encrypt::validateHash($pass, $hash);

Static Methods

::createHash($password [string])

Returns a hash of a given argument $password., (*70)

::validateHash($password [string], $hash [string])

Validates hash and a given argument $password and returns a boolean., (*71)

Session Class

A static class to handle session, (*72)

Static Methods

::setup($domain [string], $prefix [string], $host [string], $port [number], $ttl [number])

$ttl in seconds., (*73)

::get()
::set($value [mixed])
::delete()

ExceptionHandler Class

A static class to resiter and handle uncaught exceptions., (*74)

Static Methods

::add($func [string], $class [*mixed])

The Versions

08/01 2016

dev-master

9999999-dev

A light weight router, memcache + SQL handling, and session management library.

  Sources   Download

MIT

The Requires

  • php >= 5.3.0

 

19/10 2015

dev-develop

dev-develop

A light weight router, memcache + SQL handling, and session management library.

  Sources   Download

MIT

The Requires

  • php >= 5.3.0