/**
  * dispatch
  *
  * @method dispatch
  */
 public function dispatch()
 {
     $pathinfo_blocks = explode('/', ltrim(Atto::uri(), '\\'));
     $i = count($pathinfo_blocks) - 1;
     if ($pathinfo_blocks[$i] === '') {
         $file_names = array('index.html', 'index.php');
     } else {
         $file_names = array($pathinfo_blocks[$i]);
         $pathinfo_blocks[$i] = '';
     }
     $request_dir = implode(DS, $pathinfo_blocks);
     $hook_file = Atto::makeAccessPath(Atto::dir_hook() . $request_dir, $file_names, array('', '.php'));
     $htdocs_file = Atto::makeAccessPath(Atto::dir_htdocs() . $request_dir, $file_names);
     $huck_file = Atto::makeAccessPath(Atto::dir_huck() . $request_dir, $file_names, array('', '.php'));
     $view_file = null;
     if ($hook_file === '' && $htdocs_file === '' && $huck_file === '') {
         // not found
         $view_file = Atto::makeAccessPath(array(Atto::dir_error(), Atto::dir_atto_error()), array(404, 'etc'), array('.html', '.php'));
         $this->title = '404  ' . AttoHttpHelper::getTextByResponseCode(404);
         $this->error_message = '';
         AttoHttpHelper::setResponseCode(404);
         $this->funcs->view = array($this, '___view___');
     } else {
         if ($htdocs_file !== '' && is_dir($htdocs_file)) {
             // access to the directory
             $redirect_URIs = explode('?', Atto::requestUrl());
             $redirect_URIs[0] .= '/';
             AttoHttpHelper::redirect(implode('?', $redirect_URIs), 301);
         }
         if ($htdocs_file && is_file($htdocs_file)) {
             $this->files->view = $htdocs_file;
         }
         if ($hook_file && is_file($hook_file)) {
             $this->files->hook = $hook_file;
         }
         if ($huck_file && is_file($huck_file)) {
             $this->files->huck = $huck_file;
         }
     }
     $this->before();
     $this->hook();
     $content = $this->view();
     $this->render($content);
     $this->huck();
     $this->after();
 }
예제 #2
0
파일: atto.php 프로젝트: nulil/attophp
    /**
     * cascade
     *
     * @method cascade
     * @param mixed $callback
     * @param array $options
     * @throws Exception 
     */
    public static function cascade($callback, array $options = array())
    {
        // options init
        self::$_options = array_merge(self::$_options, $options);
        $init = create_function('', '
			require Atto::dir_atto_common() . \'init.php\';
			require Atto::dir_common() . \'init.php\';');
        $init();
        if (self::$_options['hide_gate']) {
            if (starts_with(Atto::gateScriptUri(), Atto::requestUri())) {
                AttoHttpHelper::redirect(Atto::uri());
            }
        }
        // timezone
        if (function_exists('date_default_timezone_set')) {
            date_default_timezone_set(self::$_options['admin_teimezone']);
        }
        // error_log
        if (self::$_options['error_log']) {
            self::errorHandlerRegister(array('AttoErrorToLog', 'publish'));
        }
        set_error_handler(create_function('$errno , $errstr, $errfile, $errline, $errcontext', '
				// callback
				foreach ( Atto::errorHandlerRegister() as $callback ) {
					$res = call_user_func( $callback, $errno , $errstr, $errfile, $errline, $errcontext );
					if ( $res === false ) {
						return $res;
					}
				}'));
        if (function_exists('set_exception_handler')) {
            // exception_log
            if (self::$_options['exception_log']) {
                self::exceptionHandlerRegister(array('AttoExceptionToLog', 'publish'));
            }
            set_exception_handler(create_function('$e', '
					// callback
					foreach ( Atto::exceptionHandlerRegister() as $callback ) {
						$res = call_user_func( $callback, $e );
						if ( $res === false ) {
							return $res;
						}
					}'));
        }
        // gzip
        if (function_exists('ob_gzhandler')) {
            self::publishHandlerRegister(create_function('$content, $mode', '
					$zlab_output_compression = ini_get( "zlib.output_compression" );
					if ( !$zlab_output_compression
							|| $zlab_output_compression == "0"
							|| strtolower( $zlab_output_compression ) == "off" ) {

						$content_encoding = Atto::serchHeader( "/^Content-Encoding:/iu" );
						if ( $content_encoding === false && Atto::isGzipContentType() ) {
							return ob_gzhandler( $content, $mode );
						}
					}
					return $content;'));
        }
        set_include_path(get_include_path() . PATH_SEPARATOR . Atto::dir_app());
        // callback
        if (!is_callable($callback)) {
            $callback = array($callback, '__invoke');
            // PHP < 5.3
            if (!is_callable($callback)) {
                throw new Exception('Callees of the callback is unknown');
            }
        }
        // publishHandler
        ob_start(create_function('$content, $mode', '
				// header
				Atto::overrideXpowerdBy();
				if ( Atto::isDebug() ) {
					header( "ElapsedMicrotime: " . Atto::getElapsedMicrotime() );
				}
				if ( class_exists( "AttoHttpHelper", false ) ) {
					AttoHttpHelper::outputHeaders();
				}
				// callback
				foreach ( Atto::publishHandlerRegister() as $callback ) {
					$res = call_user_func( $callback, $content, $mode );
					if ( $res !== false ) {
						$content = $res;
					}
				}
				return $content;'));
        ob_implicit_flush(false);
        try {
            self::_plugsInsertion();
            call_user_func($callback, self::uri());
        } catch (AttoAbstractHasRenderException $e) {
            $content = ob_get_contents();
            if (!$content) {
                $content = '';
            } else {
                ob_clean();
            }
            $e->render($content);
        } catch (Exception $e) {
            $render = array($e, 'render');
            if (is_callable($render)) {
                $content = ob_get_contents();
                if (!$content) {
                    $content = '';
                } else {
                    ob_clean();
                }
                call_user_func($render, $content);
            } else {
                throw $e;
            }
        }
        ob_end_flush();
    }