Irfan's Engine - A simple PHP Engine
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
src
tests
.gitignore
LICENSE
README.md
app.php
composer.json
ie
phpunit.xml

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.

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

Irfan's Engine now implements the PSR-7 classes and conforms to the validation constraints imposed. You can break out of these constraints by using:

// You can enable the hacker mode by defining this constant
define('HACKER_MODE', true);

If this constant is defined as a non false value, you can avoid all of the validations, though certain constraints can not be eliminated, which are essential for the proper functioning of the underlying system.

Now equiped with a console command ie, so that you can easily initialise a basic application.

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.

Quick Start

1. Installation

Install the latest version with

composer require irfantoor/engine

Note: Irfan's Engine requires PHP 7.0 or newer.

2. Basic Initialisation

Use the console command ie to create a basic app for you. When Irfan's Engine is installed, it will create a link to a shell command. Which can be used as follows to create an app.

$ ./ie app:init

3. Serve the app

App can be tested on basic php server using the following command:

$ ./ie app:serve

Go to http://localhost:8000 and voilà! your welcome app alive. You can start experimenting by changing or adding the routes, creating/modifying models, views, controllers etc.

Dont forget the shell command ie can be a big help in creating the models with the associated databases, middlewares, controllers or views etc.

Usage

Here are a few examples :

Hello World! - using response

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

use IrfanTOOR\Engine\Http\ServerRequest;
use IrfanTOOR\Engine\Http\Response;

$response = (new Response())
              ->withStatus(Response::STATUS_IM_A_TEAPOT)
              ->write('Hello World!')
...
$response->send();
<?php
...
(new Response())
	->withStatus(Response::STATUS_IM_A_TEAPOT)
	->write('Hello World!')
	->send();

Hello World! - using parameters from Request

<?php
...
# name passed as get variable: http://example.com/?name=alfa
# or posted through a form
$name = (new ServerRequest)->getAttribute('name', 'World');
(new Response())
	->write('Hello ' . ucfirst($name) . '!')
	->send();

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.

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

<?php
require 'vendor/autoload.php';

use IrfanTOOR\Engine\Http\Environment;
use IrfanTOOR\Debug;

Debug::enable();

$e = new Environment([
	'HTTP_HOST' => 'example.com',
	'Engine' => 'My Engine v1.0',
]);

// Environment is a case sensitive collection
$host   = $e->get('HTTP_HOST', 'localhost');
$engine = $e->get('Engine');

Debug::dump([$host, $engine]);

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:

$request  = new ServerRequest();
$response = new Response();
// $ie->addRoute('ANY', '.*', function ($request, $response){ ...

$uri =  $request->getUri();
$host = $uri->getHost();
$port = $uri->getPort();

Headers

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

# Removing a header
$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

<?php

return [
	'debug' => [
		# this is for production
		# 0 -- no Debug::dump($v) is processed

		# these are for the development
		# 1 -- elapsed time is attached to the response
		# 2 -- included files are included in the dump
		# 3 -- detail of routes and the environment is also dumped

		'level' => 0, # or can be 1, 2 or 3
	],
	'environment' 	=> [
		'REMOTE_ADDR' => '192.168.1.1',
		'HELLO' => 'WORLD',
	],
	'site' => [
		'name' => 'mysite.com',
	]
];

and then this config can be included like this:

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

$ie->config('site.name');

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 or by simply by using this class in any of your code directly as:

<?php
require "path/to/vendor/autoload.php";
use IrfanTOOR\Engine\Debug;
Debug::enable(2); # 2 is debug level

...
# You can use it to dump data etc.

Debug::dump($request);
Debug::dump($response->getHeaders());

Debug has a dependency on IrfanTOOR\Console for dumping the results on the console. Try including it in the starting index.php or bootstrap.php file so that it can detect any errors in the succeeding files.

About

Requirements Irfan's Engine works with PHP 7.0 or above.

License

Irfan's Engine is licensed under the MIT License - see the LICENSE file for details