tkachinc / engine
TkachInc Engine
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: >=7.0
- evenement/evenement: 2.0.*
- pimple/pimple: 3.0.*
- tkachinc/cli: ~1.2
Suggests
- ext-curl: *
- 2.3.1
- 2.3.0
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.12
- 2.1.11
- 2.1.10
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.8.6
- 1.8.5
- 1.8.4
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.0
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.10
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-develop
- dev-support
This package is auto-updated.
Last update: 2019-03-07 17:06:08 UTC
README
Base engine to web-service
This is base package, it does not have any helpers to DB or Session.
More helpers you can see in tkachinc/core package.
Package include only most popular and useful functional.
This package include base application, container, base controller, and services:
Services:
- AlgorithmsForCoding
- Attributes
- Config
- Handler
- Helpers
- Request
- Response
- Secure
- Validator
- Facade
- Singelton
- Multiton
- Registry
and this package has relations to tkachinc/cli
You can view example
Install
To install TkachInc\Engine - file composer.json your project must be entered in the unit require:
"tkachinc/engine": "~2.1"
After that, if you have already installed composer execute:
$ php composer.phar install tkachinc/engine
If not, then you must install the composer:
curl -sS https://getcomposer.org/installer | php
Create file compose.json
{
"require": {
"tkachinc/engine": "~2.2"
}
}
And execute command:
$ php composer.phar install tkachinc/engine
Routing
Routing is waiting at the entrance of the line router. Output object containing data Parsed
Example
if(isset($_GET['request_uri']))
{
$route = $_GET['request_uri'];
unset($_GET['request_uri']);
}
else
{
$route = '';
}
Config::getInstance()->append(['route' => $route]);
$router = new DeepRouter();
$router->setDefaultController('MyApp\BaseApiController');
$router->setDefaultMethod('index');
$router->setUrlMap(
[
'api' => [
'_controller' => 'MyApp\BaseApiController',
'_defaultMethod' => 'page404',
'_data' => [
'%s' => [
'_name' => 'version',
'_data' => [
'auth' => [
'_controller' => 'MyApp\User\Model\Controller\AuthController',
],
]
]
]
]
]
);
$router->setPrefixes(Config::getInstance()->get(['lang', 'list'], ['ru'=>1, 'en'=>1]));
$route = $router->parseRoute($route, true);
$className = $route->getClassName();
$methodName = $route->getMethodName();
$_GET['lang'] = $route->getPrefix();
if(class_exists($className))
{
$object = new $className($route);
call_user_func_array([$object, $methodName], []);
}
The result of parsing - the object Route. It contains the class, method, language after parsing.
Configuration urlMap
UrlMap Standard configuration includes only one key controller - responsible for the name of the controller, the respective identifier Route Extra options:
- _defaultMethod - the standard method for the request
- _method - called method, controller
- _controller - controller namespace
- _data - part from url
- _name - name params
Example
There url: /user/123 And configurations urlMap:
[
'user' => [
'_controller' => 'Example/User',
'_method' => 'get',
'_data' => ['%s' => ['_name' => 'id']]
]
]
After parsing the Router will contain: controller - Example/User method - get arguments - id => 123
Additionally when specifying a method argument second parseRoute - true key id with a value of 123 would be in the array $_REQUEST
Alternative router
Example
$router = new ClosureRouter('/test/123', 'GET');
$router->scope(function(ClosureRouter $router, $params){
$router->single('test', function(\TkachInc\Engine\Router\ClosureRouter $router, $params){
$router->single('123', function(\TkachInc\Engine\Router\ClosureRouter $router, $params){
var_dump('test');
});
}, 'POST');
$router->single('test', function(\TkachInc\Engine\Router\ClosureRouter $router, $params){
$router->single(':id', function(\TkachInc\Engine\Router\ClosureRouter $router, $params){
var_dump($router->attributes());
var_dump('test_2');
});
$router->single('123', function(\TkachInc\Engine\Router\ClosureRouter $router, $params){
var_dump('test3');
});
}, 'GET');
$router->single(':id', function(\TkachInc\Engine\Router\ClosureRouter $router, $params){
var_dump('test_2');
});
});
Output
array(1) {
["id"]=>
string(3) "123"
}
string(6) "test_2"
Nginx config example
server {
listen *:80; # порт http
#listen *:443; # порт http
#include /etc/nginx/ssl.conf; # подключение конфигурации ssl
server_name example.host;
root /path/to/dir;
charset utf-8;
#access_log /path/to/access.log;
log_not_found off;
#access_log off;
#error_log /path/to/error.log warn;
location / {
index index.php;
try_files $uri $uri @front_rewrite;
}
## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml|swf)$ {
access_log off;
expires 30d;
}
location @front_rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*)$ /index.php?request_uri=$1 last;
}
location ~ /\. {
deny all;
}
location ~ .php$ {
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#fastcgi_pass php-fpm:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
Autoloader
Recommended autoloader A more simple and fast autoloader than the one that goes to the composer.
- Connection time autoloader this file is ~ 0.00060 (0.00322 ~ complete initialization)
- Composer autoloader ~ 0.008054 (0.01221 ~ complete initialization)
Setting Example
It should be at the project to create a file autoload.php (an example can be taken from the example\autoload\autoload.php) This file contains two arrays, the first array - the connection of individual classes of non-standard directory. The second array - connect all libraries namespace (in support for PSR-0 and PSR-4)
Example
<?php
require_once(__DIR__."/SimpleAutoload.php");
$autoinclude = [
VENDOR_PATH.'/react/promise/src/functions_include.php',
];
$classMap = [
'LightOpenID' => VENDOR_PATH.'/lightopenid/lightopenid/openid.php',
];
$fallbackDirs = [
'Evenement' => VENDOR_PATH.'/evenement/evenement/src',
'Symfony' => VENDOR_PATH.'/gollariel/km-core/src',
'Symfony\\Component\\HttpFoundation\\' => VENDOR_PATH.'/symfony/http-foundation',
'Symfony\\Component\\Filesystem\\' => VENDOR_PATH.'/symfony/filesystem',
'Symfony\\Component\\EventDispatcher\\' => VENDOR_PATH.'/symfony/event-dispatcher',
'Twig' => VENDOR_PATH.'/twig/twig/lib',
//'Ratchet' => VENDOR_PATH.'/cboden/ratchet/src',
'React\\Stream\\' => VENDOR_PATH.'/react/stream/src',
'React\\Socket\\' => VENDOR_PATH.'/react/socket/src',
'React\\SocketClient\\' => VENDOR_PATH.'/react/socket-client/src',
'React\\Promise\\' => VENDOR_PATH.'/react/promise/src',
'React\\Http\\' => VENDOR_PATH.'/react/http/src',
'React\\HttpClient\\' => VENDOR_PATH.'/react/http-client/src',
'React\\EventLoop\\' => VENDOR_PATH.'/react/event-loop',
'React\\Dns\\' => VENDOR_PATH.'/react/dns',
'React\\ChildProcess\\' => VENDOR_PATH.'/react/child-process',
'React\\Cache\\' => VENDOR_PATH.'/react/cache',
'React\\ZMQ' => VENDOR_PATH.'/react/zmq/src',
'Ratchet\\' => VENDOR_PATH.'/cboden/ratchet/src/Ratchet',
'Guzzle\\Stream' => VENDOR_PATH.'/guzzle/stream',
'Guzzle\\Parser' => VENDOR_PATH.'/guzzle/parser',
'Guzzle\\Http' => VENDOR_PATH.'/guzzle/http',
'Guzzle\\Common' => VENDOR_PATH.'/guzzle/common',
];
foreach ($autoinclude as $item)
{
if(file_exists($item))
{
include $item;
}
}
$autoloader = new SimpleAutoload($classMap, $fallbackDirs);
spl_autoload_register([$autoloader, 'loadClass']);
File scaner.php - can create autoload.php file