2017 © Pedro Peláez
 

library engine

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

image

irfantoor/engine

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 28 Versions
  • 4 % Grown

The README.md

Irfan's Engine

A bare-minimum PHP framework, with the spirit with which the HTTP was invented, focussing on the requests and the responses. A Swiss-knife for world-wide-web., (*1)

The objective of this library is to be a Bare-minimum, Embeddable and Educative., (*2)

Irfan's Engine uses IrfanTOOR\Http which implements the psr/http-message., (*3)

Note: This documentation is just to get you started, you are encouraged to study the code and the examples in the examples folder, which might help you get going, by adding, extending or even writing your own classes and/or frameworks., (*4)

Quick Start

1. Installation

Install the latest version with, (*5)

composer require irfantoor/engine

Note: Irfan's Engine requires PHP 7.0 or newer., (*6)

Usage

You can find the code in examples folder., (*7)

hello-world.php

<?php

# php -S localhost:8000 hello-world.php

require ("autoload.php"); # Give the path/to/vendor/autoload.php

use IrfanTOOR\Engine;

$ie = new Engine(
    [
        'debug' => [
            'level' => 2
        ],
        'default' => [
            'name' => 'world',
        ]
    ]
);

# Name passed as get variable: http://localhost:8000/?name=alfa
# or posted through a form
# Check: http://localhost:8000/?name=alfa&debug=1
# Check: http://localhost:8000/?name=alfa&exception=1

$ie->addHandler(function ($request) use($ie) {
    $name = $request->getQueryParams()['name'] ?? $ie->config('default.name');

    $response = $ie->create('Response');
    $response->getBody()->write('Hello ' . ucfirst($name) . '!');

    if ($request->getQueryParams()['exception'] ?? null) {
        throw new Exception("An exception at your service!");
    }

    if ($request->getQueryParams()['debug'] ?? null) {
        # Dump
        d($request);
        d($response);

        # Dump and die!
        dd($ie);
    }

    # A response must be sent back in normal circumstances!
    return $response;
});

$ie->run();

Provider of Http Suite

Irfan's Engine uses IrfanTOOR\Http suite by default. You can use another provider by defining it through passed config., (*8)

Download the Psr compliant Http suite you want to use and define the provider in the config, for example you can use slim\psr7;, (*9)

$ composer require slim/psr7
<?php

require 'path/to/autoload.php';

use IrfanTOOR\Engine;

# Create engine
$ie = new Engine(
    [
        'http' => [
            'provider' => 'Slim\\Psr7',
        ]
    ]
);

# Add handler
$ie->addHandler(function($request) use($ie) {
    # Request received by handle will be a Slim\Psr7 Request

    # Psr\Slim7 Response
    $response = $ie->create('Response');
    $respone->write("Hello world from Slim\Psr7");
    return $response;
});

# Run ...
$ie->run();

Environment

Environment instance contains the environment variables and the headers passed, by the web server, which are automatically converted to headers and added to the request class., (*10)

Environment can be mocked by defining the 'env' element in the configuration file, or as follows, if using without the engine:, (*11)

Uri

Whenever a server request is created, a Uri containing the parsed information of the requested url is also present and can be accessed as:, (*12)

class RequestHandler
{
    protected $engine;

    function __construct($engine)
    {
        $this->engine = $engine;
    }

    function handle(RequestInterface $request): ResponseInterface
    {
        $uri =  $request->getUri();
        $host = $uri->getHost();
        $port = $uri->getPort();
        $path = $uri->getPath();
        # ...

        $response = $this->engine->create('Response');
        # ...

        return $response;
    }
}

$ie = new Engine();
$ie->addHandler(new RequestHandler($ie));
$ie->run();

Headers

# ...
# Setting a header
$response = $response
    ->withHeader('Content-Type', 'text/plain')
    ->withHeader('keywords', 'hello, world')
;

# Removing a header
$response = $response->withoutHeader('unwanted-header');

# Checking a header
if ($response->hasHeader('content-type')) {
    # Do something ...
}

# Getting a header, note that the key of headers is not case sensitive
$content_type = $response->getHeader('CONTENT-type');
# ...

Creating your config file: path/to/config.php

Create a config.php file:, (*13)

<?php

return [
    'debug' => [
        # Note: a level 4 sets the reporting_level to E_ALL, and is 0 for other levels.
        'level' => 0, # Or can be 1, 2, 3 or 4
    ],
    'environment'     => [
        'REMOTE_ADDR' => '192.168.1.1',
        'HELLO' => 'WORLD',
    ],
    'site' => [
        'name' => 'mysite.com',
    ]
];

and then this config can be included like this:, (*14)

<?php
$config = require("path/to/config.php");
$ie = new IrfanTOOR\Engine($config));

# OR preferably:
$ie = new IrfanTOOR\Engine([
    # note a debug level, provided while init can help catching the errors
    # of the config file, or any of the classes loaded in config etc.
    'debug' => [
        'level' => 1,
    ],
    'config_file' => "path/to/config.php",
]);

$ie->config('site.name'); # Returns "mysite.com"

Debugging

You can enable debugging while coding your application, a short, concise and to the point, error description and trace is dumped in case of any exception. You can enable the debugging using config if using Irfan's Engine., (*15)

<?php
require "path/to/vendor/autoload.php";

use IrfanTOOR\Engine;
$ie = new Engine(
    [
        'debug' => [
            'level'  => 2, # Can be from 0 to 4
        ]
    ]
);
# ...
# If debug level is above 0, you can use the function d() && dd() to dump a
# variable while development.
d($request);
dd($request->getHeaders());

About

Requirements Irfan's Engine works with PHP 7.3 or above., (*16)

License, (*17)

Irfan's Engine is licensed under the MIT License - see the LICENSE file for details., (*18)

The Versions

26/03 2018

dev-master

9999999-dev

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 framework php engine mini irfan irfans-engine

26/03 2018

v0.8.7

0.8.7.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 framework php mini irfans-engine

26/03 2018

v0.8.6

0.8.6.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 framework php mini irfans-engine

19/01 2018

v0.8.5

0.8.5.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 framework php mini irfans-engine

17/01 2018

v0.8.4

0.8.4.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 framework php mini irfans-engine

17/01 2018

v0.8.3

0.8.3.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 framework php mini irfans-engine

17/01 2018

v0.8.2

0.8.2.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

17/01 2018

v0.8.1

0.8.1.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

17/01 2018

v0.8

0.8.0.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

30/11 2017

v0.7.1

0.7.1.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

framework php mini irfans-engine

30/11 2017

v0.7

0.7.0.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

framework php mini irfans-engine

15/05 2017

v0.6.2

0.6.2.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

15/05 2017

v0.6.1

0.6.1.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

15/05 2017

v0.6

0.6.0.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

10/05 2017

v0.5.7

0.5.7.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

10/05 2017

v0.5.6

0.5.6.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

07/05 2017

v0.5.5

0.5.5.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

06/05 2017

v0.5.4

0.5.4.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

02/05 2017

v0.5.3

0.5.3.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

02/05 2017

v0.5.2

0.5.2.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

02/05 2017

v0.5.1

0.5.1.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

02/05 2017

v0.5

0.5.0.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

 

The Development Requires

framework php mini irfans-engine

26/04 2017

v0.4.2

0.4.2.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

framework php mini irfans-engine

26/04 2017

v0.4.1

0.4.1.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

framework php mini irfans-engine

26/04 2017

v0.4

0.4.0.0

A bare-minimum PHP framework, focussing on the requests and the responses. A swiss-knife for world-wide-web

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

framework php

16/01 2017

v0.3

0.3.0.0

A simple PHP Engine

  Sources   Download

MIT

The Requires

 

The Development Requires

php engine irfan

16/01 2017

v0.2

0.2.0.0

A simple PHP Engine

  Sources   Download

MIT

The Requires

 

The Development Requires

php engine irfan

07/01 2017

v0.1

0.1.0.0

A simple PHP Engine

  Sources   Download

MIT

The Requires