Ejemplo n.º 1
0
 /**
  * 
  * @param Exception $exception
  */
 public static function catchException($exception)
 {
     $code = $exception->getCode();
     $errorController = 'app\\controllers\\ErrorController';
     $result = ClassLoader::load($errorController, false);
     if ($result && class_exists($errorController)) {
         $controllerObj = new $errorController(new \radium\net\http\Request());
         $action = 'index';
         $controllerObj->_render['template'] = $action;
         $controllerObj->invokeMethod('_init');
         $data = call_user_func_array(array($controllerObj, $action), array($exception));
         $contentType = 'text/html';
         $output = '';
         if (is_string($data)) {
             $output = $data;
             $contentType = 'text/plain';
         } else {
             $controllerObj->invokeMethod('_finalize', $data ? array($data) : array());
             $output = $controllerObj->renderedContent();
             $contentType = $controllerObj->view->contentType();
         }
         header('Content-Type: ' . $contentType . '; charset=UTF-8');
         echo $output;
         exit;
     }
     $backtrace = $exception->getTrace();
     $line = array_shift($backtrace);
     while ($line && !isset($line['file'])) {
         $line = array_shift($backtrace);
     }
     header('Content-Type: text/plain; charset=UTF-8');
     echo StringUtil::getLocalizedString('Exception was thrown. ({1}): {2} at {3} line {4} ({5} line {6})', array($code, $exception->getMessage(), $line['file'], $line['line'], $exception->getFile(), $exception->getLine())) . "\n";
     exit;
 }
Ejemplo n.º 2
0
 /**
  * データベースリソースを取得
  * @param string $name データベースリソース名
  * @return array データベース設定
  * @throws Exception
  */
 public static function get($name)
 {
     if (isset(static::$resources[$name])) {
         return new Resource(static::$resources[$name]);
     }
     throw new ErrorException(StringUtil::getLocalizedString('Database resource is not found'), CONNECT_DATABASE_ERROR);
 }
Ejemplo n.º 3
0
 /**
  * プロテクテッドなメソッドを呼ぶためのプロキシ機能
  * @param string $method メソッド名
  * @param array $args 引数
  * @throws Exception メソッドが呼べなかった場合にスローされる
  */
 public final function invokeMethod($method, array $args = array())
 {
     if (is_callable(array($this, $method))) {
         call_user_func_array(array($this, $method), $args);
         return;
     }
     throw new ErrorException(StringUtil::getLocalizedString('Method "{1}" cannot be called.', array($method)), INVALID_METHOD);
 }
Ejemplo n.º 4
0
/**
 * 文字列をエスケープして出力
 * @param string $str 文字列
 * @param bool true の場合は出力しないで return する
 */
function ee($str, $return = false)
{
    $str = StringUtil::escape($str);
    if ($return) {
        return $str;
    }
    echo $str;
}
Ejemplo n.º 5
0
 /**
  * 文字列の HTML エスケープ
  * @param $value エスケープする文字列 OR 配列
  * @param array $options
  */
 public function escape($value, array $options = array())
 {
     $defaults = array('escape' => true);
     $options += $defaults;
     if ($options['escape'] === false) {
         return $value;
     }
     if (is_array($value)) {
         return array_map(array($this, __FUNCTION__), $value);
     }
     return StringUtil::escape($value);
 }
Ejemplo n.º 6
0
 /**
  * コンストラクタ
  * @param \radium\net\http\Request $request
  */
 public function __construct(Request $request, Dispatcher $dispatcher = null)
 {
     $this->request = $request;
     $this->dispatcher = $dispatcher;
     $class = get_class($this);
     $class = substr($class, strrpos($class, '\\') + 1);
     $class = substr($class, 0, strrpos($class, 'Controller'));
     $this->_controller = StringUtil::uncamelcase($class);
     $this->_render = array();
     $this->_params = array();
     $headers = $request->headers;
     if (isset($headers['X-PJAX'])) {
         $this->layout = null;
     }
 }
Ejemplo n.º 7
0
 /**
  * クラスファイルの自動ローディング
  * @param string $className クラス名
  */
 public static function load($className, $fireExceptionIfNeeded = true)
 {
     //if (class_exists($className)) return;
     $classFile = static::classFile($className);
     $prefixes = array(RADIUM_PATH, RADIUM_APP_PATH . DIRECTORY_SEPARATOR . 'libraries');
     $prefixes = array_merge($prefixes, explode(':', get_include_path()));
     foreach ($prefixes as $prefix) {
         $path = $prefix . DIRECTORY_SEPARATOR . $classFile;
         if (file_exists($path) && (include_once $path)) {
             return true;
         }
     }
     if ($fireExceptionIfNeeded) {
         throw new ErrorException(StringUtil::getLocalizedString('Class file "{1}" is not found.', array($classFile)), CLASSFILE_NOT_FOUND);
     }
     return false;
 }
Ejemplo n.º 8
0
 public static function get($url, array $args = array())
 {
     $routes = static::$_routes;
     $url = urldecode($url);
     if (isset($routes[$url])) {
         $route = $routes[$url];
         $route['args'] = $args;
         return $route;
     }
     foreach ($routes as $key => $route) {
         $checkKey = $key;
         $checkDataData = array();
         $checkDataRegexp = array();
         $checkDataArg = array();
         // {:Model:Column}
         preg_match_all('/\\{:([a-zA-Z0-9]+):([_a-zA-Z0-9]+)\\}/', $key, $matches);
         if ($matches && count($matches) > 0 && count($matches[0]) > 0) {
             $n = count($matches[0]);
             for ($i = 0; $i < $n; $i++) {
                 $param = $matches[0][$i];
                 $checkKey = str_replace($param, '______data_____', $checkKey);
                 $model = $matches[1][$i];
                 $column = $matches[2][$i];
                 $modelClass = 'app\\models\\' . StringUtil::camelcase($model);
                 $result = ClassLoader::load($modelClass, false);
                 // モデルクラスが見つかった
                 if ($result === true && class_exists($modelClass)) {
                     $checkDataData[] = array('data', $modelClass, $column);
                 } else {
                     throw new NotFoundError(StringUtil::getLocalizedString('Model Class "{1}" is not found. ({2} at {2})', array($modelClass, __FILE__, __LINE__)), ACTION_NOT_FOUND);
                 }
             }
         }
         // {:args}
         $checkKey = preg_replace_callback('/\\{:args?\\}/', function ($matches) use(&$checkDataArg) {
             $checkDataArg[] = array('arg');
             return '______arg_____';
         }, $checkKey);
         // {:regexp}
         $regexpList = array();
         $checkKey = preg_replace_callback('/\\{\\:\\/((?:[-+*.?,|\\/a-zA-Z0-9]|\\\\|\\{|\\}|\\(|\\)|\\[|\\])*)\\/\\}/', function ($matches) use(&$regexpList, &$checkDataRegexp) {
             $regexp = $matches[1];
             $regexp = str_replace('\\(', '_____regexp_escape_____', $regexp);
             $regexp = str_replace('(', '(?:', $regexp);
             $regexp = str_replace('_____regexp_escape_____', '\\(', $regexp);
             $regexpList[] = $regexp;
             $checkDataRegexp[] = array('regexp');
             return '______regexp_____';
         }, $checkKey);
         // checkData を並び替えながら準備
         $checkData = array();
         preg_match_all('/______(data|arg|regexp)_____/', $checkKey, $matches);
         if ($matches && count($matches[0]) > 0) {
             for ($i = 0; $i < count($matches[0]); $i++) {
                 switch ($matches[1][$i]) {
                     case 'data':
                         $checkData[] = array_shift($checkDataData);
                         break;
                     case 'arg':
                         $checkData[] = array_shift($checkDataArg);
                         break;
                     case 'regexp':
                         $checkData[] = array_shift($checkDataRegexp);
                         break;
                 }
             }
         }
         $checkKey = preg_quote($checkKey);
         $checkKey = str_replace('/', '\\/', $checkKey);
         // {:Model:Column} を戻す
         $checkKey = str_replace('______data_____', '([^\\/]+)', $checkKey);
         // {:args} を戻す
         $checkKey = str_replace('______arg_____', '([^\\/]+)', $checkKey);
         // {:regexp} を戻す
         $checkKey = preg_replace_callback('/______regexp_____/', function ($matches) use(&$regexpList) {
             return '(' . array_shift($regexpList) . ')';
         }, $checkKey);
         $matches = null;
         if (count($checkData) > 0) {
             //echo $checkKey . "\n";
             //print_r($checkData);
             try {
                 preg_match('/^' . $checkKey . '$/', $url, $matches);
             } catch (Exception $e) {
             }
         }
         if ($matches && count($matches) > 0) {
             $resultArgs = array();
             array_shift($matches);
             $n = count($matches);
             for ($i = 0; $i < $n; $i++) {
                 $type = $checkData[$i][0];
                 $value = $matches[$i];
                 if ($type == 'data') {
                     $modelClass = $checkData[$i][1];
                     $column = $checkData[$i][2];
                     $result = null;
                     if ((!$result || count($result) == 0) && preg_match('/^\\d+(\\.\\d+)?$/', $value)) {
                         $result = $modelClass::all(array('conditions' => array($column => floatval($value))));
                     }
                     if (!$result || count($result) == 0) {
                         $result = $modelClass::all(array('conditions' => array($column => $value)));
                     }
                     if ((!$result || count($result) == 0) && preg_match('/^(true|false)$/', $value)) {
                         $result = $modelClass::all(array('conditions' => array($column => $value == 'true')));
                     }
                     //header('Content-Type: text/plain;charset=UTF-8');print_r(array('conditions' => array($column => $value) + $extra));exit;
                     if ($result && count($result) > 0) {
                         $resultArgs[] = $result;
                     } else {
                         break;
                     }
                 } else {
                     if ($type == 'arg') {
                         $resultArgs[] = $value;
                     } else {
                         if ($type == 'regexp') {
                             $resultArgs[] = $value;
                         }
                     }
                 }
             }
             if (count($resultArgs) == count($matches)) {
                 $route['args'] = $resultArgs;
                 return $route;
             }
         }
     }
     //
     $controller = null;
     $arg = array_shift($args);
     if (preg_match('/^[a-zA-Z][_a-zA-Z0-9]*$/', $arg)) {
         $controller = $arg;
         // アクションのチェック
         if (count($args) > 0) {
             $arg = array_shift($args);
             if (preg_match('/^[a-zA-Z][_a-zA-Z0-9]*$/', $arg) && !in_array($arg, explode(' ', 'render redirect content json'))) {
                 $action = $arg;
             } else {
                 $action = 'index';
                 array_unshift($args, $arg);
             }
         } else {
             $action = 'index';
             array_unshift($args, $arg);
         }
     } else {
         array_unshift($args, $arg);
     }
     // コントローラがあったら、、
     if ($controller && $action) {
         $route = array('controller' => $controller, 'action' => $action, 'args' => $args);
         return $route;
     }
     return false;
 }
Ejemplo n.º 9
0
 /**
  *
  */
 public function dispatch($controller = null, $action = null, $args = null)
 {
     $containsController = $controller == null;
     $request = $this->request;
     if (is_null($controller)) {
         $controller = $request->params['controller'];
         $action = $request->params['action'];
         $args = $request->params['args'];
     } elseif (is_null($action)) {
         $action = $request->params['action'];
         $args = $request->params['args'];
     } elseif (is_null($args)) {
         $args = $request->params['args'];
     }
     // コントローラクラス
     $controllerClass = '\\app\\controllers\\' . StringUtil::camelcase($controller) . 'Controller';
     $result = ClassLoader::load($controllerClass, false);
     // コントローラクラスが見つからない!
     if ($result === false || !class_exists($controllerClass)) {
         throw new NotFoundError(StringUtil::getLocalizedString('Controller "{1}" is not found.', array($controllerClass)), CONTROLLER_NOT_FOUND);
     }
     // コントローラの処理
     $controllerObj = new $controllerClass($request, $this);
     $callAction = $action;
     // アクションがない場合は _global をコール
     if (!is_callable(array($controllerObj, $action)) && is_callable(array($controllerObj, '_global'))) {
         $callAction = '_global';
     } elseif (!is_callable(array($controllerObj, $action))) {
         throw new NotFoundError(StringUtil::getLocalizedString('Action "{1}" is not found. (Controller: {2})', array($action, $controllerClass)), ACTION_NOT_FOUND);
     }
     $controllerObj->template = $action;
     $controllerObj->invokeMethod('_init');
     $data = @call_user_func_array(array($controllerObj, $callAction), $args);
     if ($containsController) {
         return array($controllerObj, $data);
     }
     return $data;
 }
Ejemplo n.º 10
0
 /**
  * テンプレートの処理を行う
  * @param string $template テンプレートファイルのパス
  * @return string レンダリング結果
  */
 private function _render($template)
 {
     // テンプレートが見つからない!
     if (!file_exists($template)) {
         throw new ErrorException(StringUtil::getLocalizedString('Template "{1}" is not found.', array($template)), TEMPLATE_NOT_FOUND);
     }
     $html = $this->html;
     $form = $this->form;
     extract($this->data, EXTR_OVERWRITE);
     ob_start();
     try {
         require $template;
     } catch (Exception $e) {
         ob_clean();
         throw $e;
     }
     return ob_get_clean();
 }
Ejemplo n.º 11
0
 /**
  * 
  * @param string $name
  * @throws \Exception
  */
 public function __get($name)
 {
     switch ($name) {
         case 'baseURI':
             return $this->_config['domain'] . $this->_config['appBaseURI'];
         case 'uri':
             return $this->_config['uri'];
         case 'data':
             return $this->_config['data'];
         case 'query':
             return $this->_config['query'];
         case 'queryString':
             return $this->_config['queryString'];
         case 'headers':
             return $this->_config['headers'];
     }
     throw new ErrorException(StringUtil::getLocalizedString('{1} is an illegal property.', array($name)), INVALID_PROPERTY);
 }