diff --git a/Load.php b/Load.php index 070c72c..c82b6c3 100644 --- a/Load.php +++ b/Load.php @@ -12,26 +12,53 @@ class Load { + static protected $file; static protected $autoload; - - static public function autoload($class) + + static public function setAutoloadFrom($file) { - if (! isset(self::$autoload[$class])) { - throw new MJException('There is no such class "' . $class . '" in autoload.'); + self::$file = $file; + if (!file_exists(self::$file)) { + self::buildAutoload(); } - require(PATH . self::$autoload[$class]); + self::$autoload = require(self::$file); } - static public function setAutoloadFrom($file) + static public function autoload($class) { - if (! file_exists($file)) { - throw new MJException('Autoload source doesn\'t exists! Try to generate it!'); + if (isset(self::$autoload[$class])) { + require(PATH . self::$autoload[$class]); + return; + } + if (defined('DEBUG') && DEBUG == true) { + if (!isset(self::$autoload[$class])) { + self::buildAutoload(); + } + if (isset(self::$autoload[$class])) { + require(PATH . self::$autoload[$class]); + } } - self::$autoload = require($file); } static public function getFilePath($class) { return self::$autoload[$class]; } + + static protected function buildAutoload() + { + ignore_user_abort(true); + $dir = dirname(self::$file); + if (!file_exists($dir) && !mkdir($dir)) { + trigger_error('Can\'t create directory: "' . $dir . '"', E_USER_ERROR); + } + + $scan = array(PATH . '/lib', PATH . '/' . APP . '/src'); + + require_once(PATH . '/lib/core/util/AutoloadBuilder.php'); + + $builder = new AutoloadBuilder(self::$file, $scan); + $builder->build(); + ignore_user_abort(false); + } } \ No newline at end of file diff --git a/app/FrontController.php b/app/FrontController.php index 465f3c8..6ca28c7 100644 --- a/app/FrontController.php +++ b/app/FrontController.php @@ -104,24 +104,37 @@ class FrontController return $this->router; } - public function execute() { try { - $request = Env::getRequestUri(); - $route = $this->getRouter()->route($request); - if (! $route) { - throw new Exception('Route "' . $request . '" not found'); - } - - $action_class = $route->getAction(); - $action = new $action_class(); - $layout_class = $route->getLayout(); - $layout = new $layout_class(); - return $layout->fetch($action); + //try { + $request = Env::getRequestUri(); + $route = $this->getRouter()->route($request); + if (!$route) { + throw new Exception('Route "' . $request . '" not found'); + } + + $action_class = $route->getAction(); + $action = new $action_class(); + $layout_class = $route->getLayout(); + $layout = new $layout_class(); + return $layout->fetch($action); + //} catch (GeneralException $e) { + // throw $e; + //} /*catch (Exception $e) { + throw new GeneralException($e->getMessage(), $e->getCode()); + //} */ } catch (Exception $e) { - throw $e; + var_dump($e); + die('111'); + + /*if (DEBUG == true) { + return $e->toHtml(); + } else { + $layout = new ErrorLayout(); + return $layout->fetch($e->getCode()); + } */ } } } \ No newline at end of file diff --git a/exception/Error404Exception.php b/exception/Error404Exception.php new file mode 100644 index 0000000..0f6b6e0 --- /dev/null +++ b/exception/Error404Exception.php @@ -0,0 +1,23 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage exception + * @since 2010-02-26 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class Error404Exception extends GeneralException +{ + protected $code = 404; + + protected function sendHeader() + { + if (! headers_sent()) { + header('HTTP/1.0 404 Not Found'); + } + } + +} \ No newline at end of file diff --git a/exception/GeneralException.php b/exception/GeneralException.php new file mode 100644 index 0000000..9127984 --- /dev/null +++ b/exception/GeneralException.php @@ -0,0 +1,29 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage exception + * @since 2010-02-26 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class GeneralException extends Exception +{ + + protected $code = 500; + + protected function sendHeader() + { + if (!headers_sent()) { + header('HTTP/1.0 500 Internal Server Error'); + } + } + + public function toHtml() + { + $this->sendHeader(); + return 'Exception caught: ' . $this->getMessage(); + } +} \ No newline at end of file diff --git a/util/AutoloadBuilder.php b/util/AutoloadBuilder.php index 82e0d26..894c6ee 100644 --- a/util/AutoloadBuilder.php +++ b/util/AutoloadBuilder.php @@ -11,6 +11,9 @@ * @filesource $URL$ */ +/** + * @todo Refactoring writing, locking + */ class AutoloadBuilder { protected $autoload; @@ -27,7 +30,6 @@ class AutoloadBuilder public function build() { $this->openAutoload(); - echo "parsing started...\n"; // for dublicates check $classes = array(); foreach ($this->dirs as $dir) { @@ -50,13 +52,9 @@ class AutoloadBuilder if (preg_match_all($this->regex, $content, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { - echo "found {$match[0]}..."; if (array_key_exists($match[2], $classes)) { - echo " FAULT\n{$match[0]} also found in ". - $file->getRealPath(). ", skipped.\n"; continue; } - echo " OK\n"; $string = "'{$match[2]}'=>'" . $relative_path . "',\n"; $this->write($string); $classes[$match[2]] = $file->getRealPath(); @@ -64,7 +62,6 @@ class AutoloadBuilder } } } - echo "done.\n"; $this->closeAutoload(); } @@ -75,7 +72,7 @@ class AutoloadBuilder protected function closeAutoload() { - if ($this->write(");\n")) { + if ($this->write(');')) { fclose($this->handler); } } @@ -84,11 +81,9 @@ class AutoloadBuilder { if (! $this->handler) { if (! $this->handler = fopen($this->autoload, 'w')) { - echo "{$this->autoload} is not writable\n"; - die; + trigger_error("{$this->autoload} is not writable", E_USER_ERROR); } } return (bool) fwrite($this->handler, $string); } -} - \ No newline at end of file +} \ No newline at end of file