Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file AdiantiCoreApplication.php

Documentation is available at AdiantiCoreApplication.php

  1. <?php
  2. namespace Adianti\Core;
  3.  
  4. use ReflectionClass;
  5. use ReflectionMethod;
  6. use Exception;
  7. use Error;
  8. use ErrorException;
  9. use Adianti\Core\AdiantiCoreTranslator;
  10. use Adianti\Control\TPage;
  11. use Adianti\Widget\Base\TScript;
  12. use Adianti\Widget\Dialog\TMessage;
  13. use Adianti\Widget\Util\TExceptionView;
  14.  
  15. /**
  16.  * Basic structure to run a web application
  17.  *
  18.  * @version    7.4
  19.  * @package    core
  20.  * @author     Pablo Dall'Oglio
  21.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  22.  * @license    http://www.adianti.com.br/framework-license
  23.  */
  24. {
  25.     private static $router;
  26.     private static $request_id;
  27.     private static $debug;
  28.     
  29.     /**
  30.      * Execute class/method based on request
  31.      *
  32.      * @param $debug Activate Exception debug
  33.      */
  34.     public static function run($debug FALSE)
  35.     {
  36.         self::$request_id uniqid();
  37.         self::$debug $debug;
  38.         
  39.         $ini AdiantiApplicationConfig::get();
  40.         $service = isset($ini['general']['request_log_service']$ini['general']['request_log_service''\SystemRequestLogService';
  41.         $class   = isset($_REQUEST['class'])    $_REQUEST['class']   '';
  42.         $static  = isset($_REQUEST['static'])   $_REQUEST['static']  '';
  43.         $method  = isset($_REQUEST['method'])   $_REQUEST['method']  '';
  44.         
  45.         $content '';
  46.         set_error_handler(array('AdiantiCoreApplication''errorHandler'));
  47.         
  48.         if (!empty($ini['general']['request_log']&& $ini['general']['request_log'== '1')
  49.         {
  50.             if (empty($ini['general']['request_log_types']|| strpos($ini['general']['request_log_types']'web'!== false)
  51.             {
  52.                 self::$request_id $service::register'web');
  53.             }
  54.         }
  55.         
  56.         self::filterInput();
  57.         
  58.         $rc new ReflectionClass($class)
  59.         
  60.         if (in_array(strtolower($class)array_map('strtolower'AdiantiClassMap::getInternalClasses()) ))
  61.         {
  62.             ob_start();
  63.             new TMessage'error'AdiantiCoreTranslator::translate('The internal class ^1 can not be executed'" <b><i><u>{$class}</u></i></b>") );
  64.             $content ob_get_contents();
  65.             ob_end_clean();
  66.         }
  67.         else if (!$rc->isUserDefined())
  68.         {
  69.             ob_start();
  70.             new TMessage'error'AdiantiCoreTranslator::translate('The internal class ^1 can not be executed'" <b><i><u>{$class}</u></i></b>") );
  71.             $content ob_get_contents();
  72.             ob_end_clean();
  73.         }
  74.         else if (class_exists($class))
  75.         {
  76.             if ($static)
  77.             {
  78.                 $rf new ReflectionMethod($class$method);
  79.                 if ($rf-> isStatic ())
  80.                 {
  81.                     call_user_func(array($class$method)$_REQUEST);
  82.                 }
  83.                 else
  84.                 {
  85.                     call_user_func(array(new $class($_REQUEST)$method)$_REQUEST);
  86.                 }
  87.             }
  88.             else
  89.             {
  90.                 try
  91.                 {
  92.                     $page new $class$_REQUEST );
  93.                     
  94.                     ob_start();
  95.                     $page->show$_REQUEST );
  96.                     $content ob_get_contents();
  97.                     ob_end_clean();
  98.                 }
  99.                 catch (Exception $e)
  100.                 {
  101.                     ob_start();
  102.                     if ($debug)
  103.                     {
  104.                         new TExceptionView($e);
  105.                         $content ob_get_contents();
  106.                     }
  107.                     else
  108.                     {
  109.                         new TMessage('error'$e->getMessage());
  110.                         $content ob_get_contents();
  111.                     }
  112.                     ob_end_clean();
  113.                 }
  114.                 catch (Error $e)
  115.                 {
  116.                     
  117.                     ob_start();
  118.                     if ($debug)
  119.                     {
  120.                         new TExceptionView($e);
  121.                         $content ob_get_contents();
  122.                     }
  123.                     else
  124.                     {
  125.                         new TMessage('error'$e->getMessage());
  126.                         $content ob_get_contents();
  127.                     }
  128.                     ob_end_clean();
  129.                 }
  130.             }
  131.         }
  132.         else if (!empty($class))
  133.         {
  134.             new TMessage('error'AdiantiCoreTranslator::translate('Class ^1 not found'" <b><i><u>{$class}</u></i></b>"'.<br>' AdiantiCoreTranslator::translate('Check the class name or the file name').'.');
  135.         }
  136.         
  137.         if (!$static)
  138.         {
  139.             echo TPage::getLoadedCSS();
  140.         }
  141.         echo TPage::getLoadedJS();
  142.         
  143.         echo $content;
  144.     }
  145.     
  146.     /**
  147.      * Execute internal method
  148.      */
  149.     public static function execute($class$method$request$endpoint null)
  150.     {
  151.         self::$request_id uniqid();
  152.         
  153.         $ini AdiantiApplicationConfig::get();
  154.         $service = isset($ini['general']['request_log_service']$ini['general']['request_log_service''\SystemRequestLogService'
  155.         
  156.         if (!empty($ini['general']['request_log']&& $ini['general']['request_log'== '1')
  157.         {
  158.             if (empty($endpoint|| empty($ini['general']['request_log_types']|| strpos($ini['general']['request_log_types']$endpoint!== false)
  159.             {
  160.                 self::$request_id $service::register$endpoint );
  161.             }
  162.         }
  163.         
  164.         if (class_exists($class))
  165.         {
  166.             $rc new ReflectionClass($class);
  167.             
  168.             if (in_array(strtolower($class)array_map('strtolower'AdiantiClassMap::getInternalClasses()) ))
  169.             {
  170.                 throw new Exception(AdiantiCoreTranslator::translate('The internal class ^1 can not be executed'$class ));
  171.             }
  172.             else if (!$rc->isUserDefined())
  173.             {
  174.                 throw new Exception(AdiantiCoreTranslator::translate('The internal class ^1 can not be executed'$class ));
  175.             }
  176.             
  177.             if (method_exists($class$method))
  178.             {
  179.                 $rf new ReflectionMethod($class$method);
  180.                 if ($rf-> isStatic ())
  181.                 {
  182.                     $response call_user_func(array($class$method)$request);
  183.                 }
  184.                 else
  185.                 {
  186.                     $response call_user_func(array(new $class($request)$method)$request);
  187.                 }
  188.                 return $response;
  189.             }
  190.             else
  191.             {
  192.                 throw new Exception(AdiantiCoreTranslator::translate('Method ^1 not found'"$class::$method"));
  193.             }
  194.         }
  195.         else
  196.         {
  197.             throw new Exception(AdiantiCoreTranslator::translate('Class ^1 not found'$class));
  198.         }
  199.     }
  200.     
  201.     /**
  202.      * Filter specific framework commands
  203.      */
  204.     public static function filterInput()
  205.     {
  206.         if ($_REQUEST)
  207.         {
  208.             foreach ($_REQUEST as $key => $value)
  209.             {
  210.                 if (is_scalar($value))
  211.                 {
  212.                     if ( (substr(strtoupper($value),0,7== '(SELECT'OR (substr(strtoupper($value),0,6== 'NOESC:'))
  213.                     {
  214.                         $_REQUEST[$key'';
  215.                         $_GET[$key]     '';
  216.                         $_POST[$key]    '';
  217.                     }
  218.                 }
  219.                 else if (is_array($value))
  220.                 {
  221.                     foreach ($value as $sub_key => $sub_value)
  222.                     {
  223.                         if (is_scalar($sub_value))
  224.                         {
  225.                             if ( (substr(strtoupper($sub_value),0,7== '(SELECT'OR (substr(strtoupper($sub_value),0,6== 'NOESC:'))
  226.                             {
  227.                                 $_REQUEST[$key][$sub_key'';
  228.                                 $_GET[$key][$sub_key]     '';
  229.                                 $_POST[$key][$sub_key]    '';
  230.                             }
  231.                         }
  232.                     }
  233.                 }
  234.             }
  235.         }
  236.     }
  237.     
  238.     /**
  239.      * Set router callback
  240.      */
  241.     public static function setRouter(Callable $callback)
  242.     {
  243.         self::$router $callback;
  244.     }
  245.     
  246.     /**
  247.      * Get router callback
  248.      */
  249.     public static function getRouter()
  250.     {
  251.         return self::$router;
  252.     }
  253.     
  254.     /**
  255.      * Execute a specific method of a class with parameters
  256.      *
  257.      * @param $class class name
  258.      * @param $method method name
  259.      * @param $parameters array of parameters
  260.      */
  261.     public static function executeMethod($class$method NULL$parameters NULL)
  262.     {
  263.         self::gotoPage($class$method$parameters);
  264.     }
  265.     
  266.     /**
  267.      * Process request and insert the result it into template
  268.      */
  269.     public static function processRequest($template)
  270.     {
  271.         ob_start();
  272.         AdiantiCoreApplication::run();
  273.         $content ob_get_contents();
  274.         ob_end_clean();
  275.         
  276.         $template str_replace('{content}'$content$template);
  277.         
  278.         return $template;
  279.     }
  280.      
  281.     /**
  282.      * Goto a page
  283.      *
  284.      * @param $class class name
  285.      * @param $method method name
  286.      * @param $parameters array of parameters
  287.      */
  288.     public static function gotoPage($class$method NULL$parameters NULL$callback NULL)
  289.     {
  290.         unset($parameters['static']);
  291.         $query self::buildHttpQuery($class$method$parameters);
  292.         
  293.         TScript::create("__adianti_goto_page('{$query}');"true1);
  294.     }
  295.     
  296.     /**
  297.      * Load a page
  298.      *
  299.      * @param $class class name
  300.      * @param $method method name
  301.      * @param $parameters array of parameters
  302.      */
  303.     public static function loadPage($class$method NULL$parameters NULL)
  304.     {
  305.         $query self::buildHttpQuery($class$method$parameters);
  306.         
  307.         TScript::create("__adianti_load_page('{$query}');"true1);
  308.     }
  309.     
  310.     /**
  311.      * Load a page url
  312.      *
  313.      * @param $class class name
  314.      * @param $method method name
  315.      * @param $parameters array of parameters
  316.      */
  317.     public static function loadPageURL($query)
  318.     {
  319.         TScript::create("__adianti_load_page('{$query}');"true1);
  320.     }
  321.     
  322.     /**
  323.      * Post data
  324.      *
  325.      * @param $class class name
  326.      * @param $method method name
  327.      * @param $parameters array of parameters
  328.      */
  329.     public static function postData($formName$class$method NULL$parameters NULL)
  330.     {
  331.         $url array();
  332.         $url['class']  $class;
  333.         $url['method'$method;
  334.         unset($parameters['class']);
  335.         unset($parameters['method']);
  336.         $url array_merge($url(array) $parameters);
  337.         
  338.         TScript::create("__adianti_post_data('{$formName}', '".http_build_query($url)."');");
  339.     }
  340.     
  341.     /**
  342.      * Build HTTP Query
  343.      *
  344.      * @param $class class name
  345.      * @param $method method name
  346.      * @param $parameters array of parameters
  347.      */
  348.     public static function buildHttpQuery($class$method NULL$parameters NULL)
  349.     {
  350.         $url [];
  351.         $url['class']  $class;
  352.         if ($method)
  353.         {
  354.             $url['method'$method;
  355.         }
  356.         
  357.         if (!empty($parameters['class']&& $parameters['class'!== $class)
  358.         {
  359.             $parameters['previous_class'$parameters['class'];
  360.         }
  361.         
  362.         if (!empty($parameters['method']&& $parameters['method'!== $method)
  363.         {
  364.             $parameters['previous_method'$parameters['method'];
  365.         }
  366.         
  367.         unset($parameters['class']);
  368.         unset($parameters['method']);
  369.         $query http_build_query($url);
  370.         $callback self::$router;
  371.         $short_url null;
  372.         
  373.         if ($callback)
  374.         {
  375.             $query  $callback($queryTRUE);
  376.         }
  377.         else
  378.         {
  379.             $query 'index.php?'.$query;
  380.         }
  381.         
  382.         if (strpos($query'?'!== FALSE)
  383.         {
  384.             return $query ( (is_array($parameters&& count($parameters)>0'&'.http_build_query($parameters'' );
  385.         }
  386.         else
  387.         {
  388.             return $query ( (is_array($parameters&& count($parameters)>0'?'.http_build_query($parameters'' );
  389.         }
  390.     }
  391.     
  392.     /**
  393.      * Reload application
  394.      */
  395.     public static function reload()
  396.     {
  397.         TScript::create("__adianti_goto_page('index.php')");
  398.     }
  399.     
  400.     /**
  401.      * Register URL
  402.      *
  403.      * @param $page URL to be registered
  404.      */
  405.     public static function registerPage($page)
  406.     {
  407.         TScript::create("__adianti_register_state('{$page}', 'user');");
  408.     }
  409.     
  410.     /**
  411.      * Handle Catchable Errors
  412.      */
  413.     public static function errorHandler($errno$errstr$errfile$errline)
  414.     {
  415.         if $errno === E_RECOVERABLE_ERROR )
  416.         {
  417.             throw new ErrorException($errstr$errno0$errfile$errline);
  418.         }
  419.         
  420.         return false;
  421.     }
  422.     
  423.     /**
  424.      * Get request headers
  425.      */
  426.     public static function getHeaders()
  427.     {
  428.         $headers array();
  429.         foreach ($_SERVER as $key => $value)
  430.         {
  431.             if (substr($key05== 'HTTP_')
  432.             {
  433.                 $header str_replace(' ''-'ucwords(str_replace('_'' 'strtolower(substr($key5)))));
  434.                 $headers[$header$value;
  435.             }
  436.         }
  437.         
  438.         if (function_exists('getallheaders'))
  439.         {
  440.             $allheaders getallheaders();
  441.             
  442.             if ($allheaders)
  443.             {
  444.                 return $allheaders;
  445.             }
  446.             
  447.             return $headers;
  448.         }
  449.         return $headers;
  450.     }
  451.     
  452.     /**
  453.      * Returns the execution id
  454.      */
  455.     public static function getRequestId()
  456.     {
  457.         return self::$request_id;
  458.     }
  459.     
  460.     /**
  461.      * Returns the debug mode
  462.      */
  463.     public static function getDebugMode()
  464.     {
  465.         return self::$debug;
  466.     }
  467. }