Skip to content
This repository was archived by the owner on Oct 16, 2018. It is now read-only.

Commit 2bf14e0

Browse files
committed
INITIAL Commit
0 parents  commit 2bf14e0

17 files changed

+474
-0
lines changed

bundle/App/API/DefaultAPI.php.fabs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace {{NamespaceName}}\App\API;
4+
5+
use {{NamespaceName}}\App\Core\APIBase;
6+
7+
class DefaultAPI extends APIBase
8+
{
9+
/**
10+
* @return string
11+
*/
12+
public function getPrefix()
13+
{
14+
return '/';
15+
}
16+
17+
public function get()
18+
{
19+
return [
20+
'hello' => 'world'
21+
];
22+
}
23+
}

bundle/App/Config/Configs.php.fabs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace {{NamespaceName}}\App\Config;
5+
6+
use Fabs\Serialize\SerializableObject;
7+
8+
class Configs extends SerializableObject
9+
{
10+
/** @var string */
11+
public $default_namespace = null;
12+
/** @var string[] */
13+
public $loader_namespaces = [];
14+
/** @var string[] */
15+
public $loader_directories = [];
16+
/** @var string[] */
17+
public $task_namespaces = [];
18+
/** @var string[] */
19+
public $api_namespaces = [];
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: fabsolutely
5+
* Date: 16/07/2017
6+
* Time: 14:49
7+
*/
8+
9+
namespace {{NamespaceName}}\App\Constant;
10+
11+
12+
class Services
13+
{
14+
const CONFIG = 'config';
15+
const CACHE = 'cache';
16+
const AUTOLOAD_HANDLER = 'autoload_handler';
17+
}

bundle/App/Core/APIBase.php.fabs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace {{NamespaceName}}\App\Core;
4+
use {{NamespaceName}}\App\Config\Configs;
5+
6+
/**
7+
* Class APIBase
8+
* @package {{NamespaceName}}\App\Core
9+
*
10+
* @property Configs config
11+
*/
12+
abstract class APIBase extends \Fabs\Rest\APIBase
13+
{
14+
15+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace {{NamespaceName}}\App\Core;
4+
5+
use Fabs\LINQ\LINQ;
6+
use {{NamespaceName}}\App\Config\Configs;
7+
use {{NamespaceName}}\App\Library\ExceptionHandler;
8+
use Phalcon\Loader;
9+
10+
/**
11+
* Class AutoloadHandler
12+
* @package {{NamespaceName}}\App\Core
13+
*
14+
* @property Configs config
15+
*/
16+
class AutoloadHandler extends \Fabs\Rest\Services\AutoloadHandler
17+
{
18+
public function __construct()
19+
{
20+
$this->registerDirs();
21+
$this->registerNamespaces();
22+
set_exception_handler([ExceptionHandler::class, 'handle']);
23+
}
24+
25+
private function registerDirs()
26+
{
27+
$loader = new Loader();
28+
$loader_dirs = LINQ::from($this->config->loader_directories)
29+
->select(function ($loader_directory) {
30+
return APP_PATH . $loader_directory;
31+
})
32+
->toArray();
33+
$loader->registerDirs($loader_dirs);
34+
$loader->register();
35+
}
36+
37+
private function registerNamespaces()
38+
{
39+
$loader = new Loader();
40+
$namespace_dirs = [];
41+
42+
$loader_namespaces = $this->config->loader_namespaces;
43+
44+
foreach ($loader_namespaces as $namespace) {
45+
$namespace_cleaned = str_replace('\\', '/', str_replace($this->config->default_namespace, '', $namespace));
46+
$namespace_dirs[$namespace] = BASE_PATH . $namespace_cleaned;
47+
}
48+
$loader->registerNamespaces($namespace_dirs);
49+
$loader->register();
50+
51+
52+
if (PHP_SAPI === 'cli') {
53+
$pre_load_namespaces = $this->config->task_namespaces;
54+
} else {
55+
$pre_load_namespaces = $this->config->api_namespaces;
56+
}
57+
58+
foreach ($pre_load_namespaces as $namespace) {
59+
$namespace_cleaned = str_replace('\\', '/', str_replace($this->config->default_namespace, '', $namespace));
60+
$this->registerNamespace($namespace, BASE_PATH . $namespace_cleaned);
61+
}
62+
}
63+
}

bundle/App/Core/DI.php.fabs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: fabsolutely
5+
* Date: 16/07/2017
6+
* Time: 14:49
7+
*/
8+
9+
namespace {{NamespaceName}}\App\Core;
10+
11+
12+
use {{NamespaceName}}\App\Config\Configs;
13+
use {{NamespaceName}}\App\Constant\Services;
14+
15+
class DI extends \Fabs\Rest\DI
16+
{
17+
public function __construct()
18+
{
19+
parent::__construct();
20+
21+
$is_debug = getenv("DEBUG_MODE");
22+
if ($is_debug !== false) {
23+
if ($is_debug === "true") {
24+
$is_debug = true;
25+
} else {
26+
$is_debug = false;
27+
}
28+
}
29+
30+
$this->setShared(Services::AUTOLOAD_HANDLER,function(){
31+
return new AutoloadHandler();
32+
});
33+
34+
$this->setShared(Services::CONFIG, function () use ($is_debug) {
35+
if ($is_debug) {
36+
$file = json_decode(file_get_contents(BASE_PATH . '/config/configs.debug.json'), true);
37+
} else {
38+
$file = json_decode(file_get_contents(BASE_PATH . '/config/configs.json'), true);
39+
}
40+
return Configs::deserialize($file);
41+
});
42+
43+
$this->setShared(Services::CACHE, function () {
44+
$frontCache = new \Phalcon\Cache\Frontend\Data(
45+
[
46+
'lifetime' => 172800,
47+
]
48+
);
49+
50+
$cache = new \Phalcon\Cache\Backend\File(
51+
$frontCache,
52+
[
53+
'cacheDir' => BASE_PATH . '/cache/',
54+
]
55+
);
56+
57+
return $cache;
58+
});
59+
}
60+
}

bundle/App/Core/TaskBase.php.fabs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace {{NamespaceName}}\App\Core;
4+
5+
use {{NamespaceName}}\App\Config\Configs;
6+
/**
7+
* Class TaskBase
8+
* @package {{NamespaceName}}\App\Core
9+
*
10+
* @property Configs config
11+
*/
12+
abstract class TaskBase extends \Fabs\Rest\TaskBase
13+
{
14+
15+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace {{NamespaceName}}\App\Library;
4+
5+
use ErrorException;
6+
use Exception;
7+
use Fabs\Rest\Services\HttpStatusCodeHandler;
8+
use Fabs\Serialize\Validation\ValidationException;
9+
use Monolog\Formatter\LineFormatter;
10+
use Monolog\Handler\StreamHandler;
11+
use Monolog\Logger;
12+
13+
class ExceptionHandler
14+
{
15+
/**
16+
* @param Exception $exception
17+
*/
18+
public static function handle($exception)
19+
{
20+
try {
21+
if ($exception instanceof ValidationException) {
22+
$logger = self::getLogger('validation');
23+
$log = sprintf(
24+
"\n\nfile: %s:%s\nproperty: %s validation: %s \nstacktrace: %s \ninputs: %s\ncontext: %s\n\n",
25+
$exception->getFile(),
26+
(string)$exception->getLine(),
27+
$exception->getPropertyName(),
28+
$exception->getValidatorName(),
29+
$exception->getTraceAsString(),
30+
file_get_contents('php://input'),
31+
json_encode($_SERVER)
32+
);
33+
$logger->error($log);
34+
(new HttpStatusCodeHandler())->internalServerError();
35+
} else {
36+
if ($exception instanceof ErrorException) {
37+
$logger = self::getLogger('error');
38+
} else {
39+
$logger = self::getLogger('exception');
40+
}
41+
$log = sprintf(
42+
"\n\nfile: %s:%s\nmessage: %s \nstacktrace: %s \ninputs: %s\ncontext: %s\n\n",
43+
$exception->getFile(),
44+
(string)$exception->getLine(),
45+
$exception->getMessage(),
46+
$exception->getTraceAsString(),
47+
file_get_contents('php://input'),
48+
json_encode($_SERVER)
49+
);
50+
$logger->error($log);
51+
(new HttpStatusCodeHandler())->internalServerError();
52+
}
53+
} catch (\Exception $exception) {
54+
// do nothing
55+
}
56+
}
57+
private static function getLogger($name)
58+
{
59+
$logger = new Logger($name);
60+
$formatter = new LineFormatter("%datetime% > %level_name% > %message% %context% %extra%\n", "Y-m-d H:i:s", true);
61+
$handler = new StreamHandler(BASE_PATH . '/logs/' . $name . '.log', Logger::ERROR);
62+
$handler->setFormatter($formatter);
63+
$logger->pushHandler($handler);
64+
return $logger;
65+
}
66+
}

bundle/App/Task/TestTask.php.fabs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: fabsolutely
5+
* Date: 16/07/2017
6+
* Time: 15:51
7+
*/
8+
9+
namespace {{NamespaceName}}\App\Task;
10+
11+
12+
use {{NamespaceName}}\App\Core\TaskBase;
13+
14+
class TestTask extends TaskBase
15+
{
16+
17+
public function getName()
18+
{
19+
return 'test';
20+
}
21+
22+
public function execute()
23+
{
24+
return ['task' => 'success'];
25+
}
26+
}

bundle/bootstrap.php.fabs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
define('BASE_PATH', __DIR__);
4+
define('APP_PATH', BASE_PATH . '/App');
5+
require_once BASE_PATH . '/vendor/autoload.php';
6+
$loader = new \Phalcon\Loader();
7+
$loader->registerNamespaces([
8+
'{{NamespaceName}}\\App\\Constant' => APP_PATH . '/Constant',
9+
'{{NamespaceName}}\\App\\Core' => APP_PATH . '/Core',
10+
'{{NamespaceName}}\\App\\Config' => APP_PATH . '/Config'
11+
])->register();

0 commit comments

Comments
 (0)