A super easy PHP Framework for web development! https://github.com/exacti/phacil-framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

573 lines
15 KiB

6 years ago
<?php
/**
* Copyright (c) 2019. ExacTI Technology Solutions
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/
6 years ago
namespace Phacil\Framework;
3 years ago
/**
3 years ago
* Start engine class
3 years ago
*
3 years ago
* @since 1.0.0
3 years ago
* @package Phacil\Framework
* @property \Phacil\Framework\Api\Database $db
3 years ago
*/
final class startEngineExacTI {
/**
3 years ago
* Storage the PHP version
*
* @var string|false
*/
public $phpversion;
//protected $includes;
/**
3 years ago
* Loaded paths to autoload.
*
3 years ago
* @see engine/autoload.php
* @since 1.3.0
* @deprecated 2.0.0
* @var array
*/
3 years ago
protected $dirs = [];
/**
3 years ago
* Instance of all engine elements
*
* @var Registry
3 years ago
*
* @since 1.0.0
*/
public $registry;
/**
*
* @var \Phacil\Framework\Registry
*/
static private $RegistryAlt;
/**
3 years ago
* System pre actions loader
*
3 years ago
* @var false|ActionSystem|\Phacil\Framework\Interfaces\Action
* @since 1.5.1
*/
private $preActions = false;
/**
* Composer object
*
* @var \Composer\Autoload\ClassLoader|false
*/
private $composer = false;
/**
*
* @var \Phacil\Framework\startEngineExacTI
*/
static private $instance;
/**
* @return void
* @throws Exception
* @throws TypeError
*/
public function __construct () {
// Check Version
$this->phpversion = $this->checkPHPversion();
//Check Config Load
$loadConfig = $this->checkConfigFile();
if($loadConfig) {
$this->defineAuxConstants();
}
if(defined('DEBUG') && DEBUG == true) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
$this->loadengine();
// Registry
$this->registry = new Registry();
self::$RegistryAlt = &$this->registry;
$this->request = new Request();
10 months ago
\Phacil\Framework\Registry::addPreference(\Phacil\Framework\Config::DIR_SYSTEM()."etc/preferences.json");
\Phacil\Framework\Registry::addPreferenceByRoute(self::getRoute());
if($this->composer) {
$this->registry->set('composer', $this->composer);
}
}
static public function getRoute() {
if (Request::GET('route')) {
return (Request::GET('route'));
} else {
$default = \Phacil\Framework\Config::DEFAULT_ROUTE() ?: \Phacil\Framework\Config::DEFAULT_ROUTE('common/home');
return Request::GET('route', $default);
}
return Request::GET('route') ?: (\Phacil\Framework\Config::DEFAULT_ROUTE() ?: \Phacil\Framework\Config::DEFAULT_ROUTE('common/home'));
}
/**
*
* @return \Phacil\Framework\startEngineExacTI
*/
static public function getInstance() {
if(!self::$instance)
self::$instance = new self();
return self::$instance;
}
/**
* @param \Phacil\Framework\startEngineExacTI $instance
* @return void
*/
static public function setInstance(startEngineExacTI $instance) {
self::$instance = &$instance;
}
/**
* @param string $key
* @return object
*/
public function __get($key) {
return $this->registry->get($key);
}
/**
* @param string $key
* @param object $value
* @return void
*/
public function __set($key, $value) {
$this->registry->set($key, $value);
}
3 years ago
/**
* Check if system have a minimum PHP requirement
* @since 1.0.0
* @return string|false
*/
private function checkPHPversion() {
if (version_compare(phpversion(), '5.4.40', '>') == false) {
3 years ago
trigger_error('PHP 5.4.40+ Required', E_ERROR);
} else {
return phpversion();
}
}
3 years ago
/**
* Check if config file is present loadable
* @return true|void
* @since 1.0.0
*/
private function checkConfigFile() {
if (!$this->checkConstantsRequired()) {
try {
$baseDir = str_replace('system', '', __DIR__);
include_once($baseDir."config.php");
if (!$this->checkConstantsRequired()) {
throw new \Exception("Can't load minimun config constants, please check your config file!");
}
} catch (Exception $e) {
exit($e->getMessage());
}
}
return true;
}
3 years ago
/**
* Check if have the required minimum configurations
* @return bool
* @since 1.0.0
*/
private function checkConstantsRequired () {
return !(!defined('DIR_APPLICATION') || !defined('DIR_SYSTEM') || !defined('DIR_PUBLIC') || !defined('DIR_TEMPLATE') || !defined('USE_DB_CONFIG'));
}
3 years ago
/**
* Define compatibility constants
* @return void
* @since 1.0.0
*/
private function defineAuxConstants () {
(defined('HTTP_URL')) ? define('HTTP_SERVER', HTTP_URL) : '';
(defined('HTTPS_URL')) ? define('HTTPS_SERVER', HTTPS_URL) : '';
}
/**
3 years ago
* Load the autoload SPL engine
* @return void
3 years ago
* @throws Exception
* @since 1.0.0
*/
private function loadengine () {
require_once (DIR_SYSTEM.'engine/autoload.php');
if(isset($autoloadComposer)) {
$this->composer = &$autoloadComposer;
}
}
/**
3 years ago
* Define the timezone
* @param string $utc
* @return void
3 years ago
* @since 1.2.1
*/
public function setTimezone($utc) {
try {
$tzc = @date_default_timezone_set($utc);
if (!$tzc){
throw new \ErrorException($utc. " not found in PHP Compiler.");
}
} catch (\ErrorException $e) {
$trace = ($e->getTrace());
throw new Exception('Timezone Error: '. $e->getMessage() ." on ". $trace[0]['file'] ." in line ". $trace[0]['line'].".");
}
}
3 years ago
/**
* Get the timezone
* @since 1.2.1
* @return string
*/
public function getTimezone(){
return date_default_timezone_get();
}
3 years ago
/**
* List system available timezones
* @since 1.2.1
* @return array|false
*/
public function listTimezones() {
return \DateTimeZone::listIdentifiers(\DateTimeZone::ALL);
}
3 years ago
/**
* Return Phacil Framework version
* @since 1.3.1
* @return string|false
*/
public function version() {
return file_get_contents(\Phacil\Framework\Config::DIR_SYSTEM()."engine/VERSION");
}
3 years ago
/**
* Include extra registration file
* @since 1.3.2
* @return void
*/
public function extraRegistrations() {
if(file_exists(\Phacil\Framework\Config::DIR_SYSTEM()."registrations.php"))
include(\Phacil\Framework\Config::DIR_SYSTEM()."registrations.php");
}
3 years ago
/**
* Return the defined constants
* @return array
* @since 1.5.0
*/
public function constants(){
return get_defined_constants(true);
}
3 years ago
/**
* Return only user defined constants
* @return array
* @since 1.5.0
*/
public function userConstants() {
return $this->constants()['user'];
}
/**
3 years ago
* Return defined constant based as group
* @param string $constant
* @param string $group
* @return mixed
3 years ago
* @since 1.5.0
*/
public function constantName($constant, $group = 'user') {
foreach ($this->constants()[$group] as $name => $value){
if($constant === $value)
return $name;
}
return $constant;
}
3 years ago
/**
* Add controller system pre-actions
* @return array
* @since 1.5.1
*/
public function controllerPreActions() {
return (isset($this->preActions) && is_array($this->preActions)) ? $this->preActions : [];
}
/**
* Add route to pre actions array
*
* @param string $route
* @return void
* @since 2.0.0
*/
public function addControllerPreAction($route) {
$this->preActions[] = $route;
return;
}
3 years ago
/**
* Check the registry element
*
* @since 2.0.0
* @param string $key
* @return true|null
*/
public function checkRegistry($key){
//mail
if(!isset($this->registry->$key) && $key == 'mail'){
$this->mail = new Mail();
$this->mail->protocol = $this->config->get('config_mail_protocol');
if($this->config->get('config_mail_protocol') == 'smtp'){
$this->mail->parameter = $this->config->get('config_mail_parameter');
$this->mail->hostname = $this->config->get('config_smtp_host');
$this->mail->username = $this->config->get('config_smtp_username');
$this->mail->password = $this->config->get('config_smtp_password');
$this->mail->port = $this->config->get('config_smtp_port');
$this->mail->timeout = $this->config->get('config_smtp_timeout');
}
}
// Translate
if(!isset($this->registry->$key) && $key == 'translate'){
$this->translate = new Translate();
}
// Session
if(!isset($this->registry->$key) && $key == 'session'){
$this->session = new Session();
}
3 years ago
return (isset($this->registry->$key)) ?: NULL;
}
/** @return \Phacil\Framework\Registry */
static public function getRegistry() {
return self::$instance->registry;
}
6 years ago
}
/**
* @var \Phacil\Framework\startEngineExacTI $engine
* */
$engine = startEngineExacTI::getInstance();
6 years ago
// Registry
/** @var \Phacil\Framework\startEngineExacTI $engine */
$engine->engine = $engine;
6 years ago
// Loader
/**
* @var \Phacil\Framework\Interfaces\Loader
*/
$engine->load = $engine->getRegistry()->create("Phacil\Framework\Interfaces\Loader", [$engine->registry]);
6 years ago
// Config
3 years ago
/** @var Config */
$engine->config = new Config();
6 years ago
// Exception Handler
set_exception_handler(function ($e) use (&$engine) {
if ($engine->config->get('config_error_display')) {
echo '<p><strong>' . get_class($e) . '</strong>: ' . $e->getMessage() . ' in <strong><em>' . str_replace(\Phacil\Framework\Config::DIR_APPLICATION(), '', $e->getFile()) . '</em></strong> on line <strong>' . $e->getLine() . '</strong></p>';
}
if (get_class($e) != 'Phacil\Framework\Exception') {
$exception = new \Phacil\Framework\Exception();
$exception->setObject($e);
}
if ($engine->config->get('config_error_log')) {
$engine->log->write(get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine());
}
});
if(\Phacil\Framework\Config::DB_DRIVER())
$engine->db = $engine->getRegistry()->create("Phacil\Framework\Api\Database", [
\Phacil\Framework\Config::DB_DRIVER(),
\Phacil\Framework\Config::DB_HOSTNAME(),
\Phacil\Framework\Config::DB_USERNAME(),
\Phacil\Framework\Config::DB_PASSWORD(),
\Phacil\Framework\Config::DB_DATABASE()
]);
6 years ago
// Settings
if(!empty($configs)){
foreach ($configs as $key => $confValue) {
$engine->config->set($key, $confValue);
}
6 years ago
}
if(\Phacil\Framework\Config::USE_DB_CONFIG() === true) {
$query = (\Phacil\Framework\Config::CUSTOM_DB_CONFIG()) ? $engine->db->query(\Phacil\Framework\Config::CUSTOM_DB_CONFIG()) : $engine->db->query()->select()->from('settings')->orderBy('setting_id', \Phacil\Framework\MagiQL\Api\Syntax\OrderBy::ASC)->load();
6 years ago
foreach ($query as $setting) {
if (!$setting['serialized']) {
$engine->config->set($setting['key'], $setting['value']);
} else {
$engine->config->set($setting['key'], unserialize($setting['value']));
}
}
6 years ago
}
$engine->config->set('config_url', \Phacil\Framework\Config::HTTP_URL());
$engine->config->set('config_ssl', \Phacil\Framework\Config::HTTPS_URL());
6 years ago
//timezone
if($engine->config->get('date_timezone')){
$engine->setTimezone($engine->config->get('date_timezone'));
}
6 years ago
// Site Title
if($engine->config->get('PatternSiteTitle') == true) {
define('PATTERSITETITLE', $engine->config->get('PatternSiteTitle'));
6 years ago
} else {
define('PATTERSITETITLE', false);
6 years ago
}
// Url
/**
* @var \Phacil\Framework\Interfaces\Url
*/
$engine->url = $engine->getRegistry()->create("Phacil\Framework\Interfaces\Url", [$engine->config->get('config_url'), $engine->config->get('config_use_ssl') ? $engine->config->get('config_ssl') : $engine->config->get('config_url')]);
6 years ago
// Log
if(!$engine->config->get('config_error_filename')){
$engine->config->set('config_error_filename', 'error.log');
6 years ago
}
/**
* @var Log
*/
$engine->log = new Log($engine->config->get('config_error_filename'));
6 years ago
// Error Handler
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$engine){
switch ($errno) {
case E_NOTICE:
case E_USER_NOTICE:
$error = 'Notice';
break;
case E_WARNING:
case E_USER_WARNING:
$error = 'Warning';
break;
case E_ERROR:
case E_USER_ERROR:
$error = 'Fatal Error';
break;
case E_DEPRECATED:
case E_USER_DEPRECATED:
$error = 'Deprecated';
break;
default:
$error = $engine->constantName($errno, 'Core');
break;
}
if ($engine->config->get('config_error_display')) {
echo '<p><strong>' . $error . '</strong>: ' . $errstr . ' in <em>' . str_replace(\Phacil\Framework\Config::DIR_APPLICATION(), "", $errfile) . '</em> on line <strong>' . $errline . '</strong></p>';
}
if ($engine->config->get('config_error_log')) {
$engine->log->write( $error . ': ' . $errstr . ' in ' . $errfile . ' on line ' . $errline.' | Phacil '.$engine->version(). ' on PHP '.$engine->phpversion);
}
return true;
});
/**
* Caches
* @var Caches
*/
$engine->cache = new Caches();
6 years ago
/**
* Request
* @var Request
*/
//$engine->request = new Request();
6 years ago
// Response
$engine->response = new Response();
$engine->response->addHeader('Content-Type: text/html; charset=utf-8');
if($engine->config->get('config_compression'))
$engine->response->setCompression($engine->config->get('config_compression'));
6 years ago
// Session
$engine->session = new Session(
$engine->config->get('session_redis')
);
6 years ago
// Document
$engine->document = new Document();
6 years ago
// Custom registrations
$engine->extraRegistrations();
6 years ago
// Front Controller
$frontController = new Front($engine->registry);
6 years ago
// SEO URL's
$frontController->addPreAction(new Action((string) 'url/seo_url', [], \Phacil\Framework\Interfaces\Action::SYSTEM));
3 years ago
//extraPreActions
if($engine->controllerPreActions()){
foreach ($engine->controllerPreActions() as $action){
$frontController->addPreAction(new Action($action));
}
}
6 years ago
// Router
$action = new Action(startEngineExacTI::getRoute());
6 years ago
// Dispatch
$not_found = \Phacil\Framework\Config::NOT_FOUND() ?: \Phacil\Framework\Config::NOT_FOUND('error/not_found');
$frontController->dispatch($action, ($not_found));
6 years ago
// Output
$engine->response->output();