Code refactoring, #16

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@115 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
pzinovkin
2010-02-26 22:33:07 +00:00
parent 1b422d87ba
commit e33f2a8536
5 changed files with 121 additions and 34 deletions

View File

@ -12,26 +12,53 @@
class Load class Load
{ {
static protected $file;
static protected $autoload; static protected $autoload;
static public function setAutoloadFrom($file)
{
self::$file = $file;
if (!file_exists(self::$file)) {
self::buildAutoload();
}
self::$autoload = require(self::$file);
}
static public function autoload($class) static public function autoload($class)
{ {
if (! isset(self::$autoload[$class])) { if (isset(self::$autoload[$class])) {
throw new MJException('There is no such class "' . $class . '" in autoload.'); require(PATH . self::$autoload[$class]);
return;
} }
require(PATH . self::$autoload[$class]); if (defined('DEBUG') && DEBUG == true) {
} if (!isset(self::$autoload[$class])) {
self::buildAutoload();
static public function setAutoloadFrom($file) }
{ if (isset(self::$autoload[$class])) {
if (! file_exists($file)) { require(PATH . self::$autoload[$class]);
throw new MJException('Autoload source doesn\'t exists! Try to generate it!'); }
} }
self::$autoload = require($file);
} }
static public function getFilePath($class) static public function getFilePath($class)
{ {
return self::$autoload[$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);
}
} }

View File

@ -104,24 +104,37 @@ class FrontController
return $this->router; return $this->router;
} }
public function execute() public function execute()
{ {
try { try {
$request = Env::getRequestUri(); //try {
$route = $this->getRouter()->route($request); $request = Env::getRequestUri();
if (! $route) { $route = $this->getRouter()->route($request);
throw new Exception('Route "' . $request . '" not found'); if (!$route) {
} throw new Exception('Route "' . $request . '" not found');
}
$action_class = $route->getAction();
$action = new $action_class(); $action_class = $route->getAction();
$layout_class = $route->getLayout(); $action = new $action_class();
$layout = new $layout_class(); $layout_class = $route->getLayout();
return $layout->fetch($action); $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) { } 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());
} */
} }
} }
} }

View File

@ -0,0 +1,23 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @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');
}
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @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();
}
}

View File

@ -11,6 +11,9 @@
* @filesource $URL$ * @filesource $URL$
*/ */
/**
* @todo Refactoring writing, locking
*/
class AutoloadBuilder class AutoloadBuilder
{ {
protected $autoload; protected $autoload;
@ -27,7 +30,6 @@ class AutoloadBuilder
public function build() public function build()
{ {
$this->openAutoload(); $this->openAutoload();
echo "parsing started...\n";
// for dublicates check // for dublicates check
$classes = array(); $classes = array();
foreach ($this->dirs as $dir) { foreach ($this->dirs as $dir) {
@ -50,13 +52,9 @@ class AutoloadBuilder
if (preg_match_all($this->regex, $content, $matches, PREG_SET_ORDER)) { if (preg_match_all($this->regex, $content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) { foreach ($matches as $match) {
echo "found {$match[0]}...";
if (array_key_exists($match[2], $classes)) { if (array_key_exists($match[2], $classes)) {
echo " FAULT\n{$match[0]} also found in ".
$file->getRealPath(). ", skipped.\n";
continue; continue;
} }
echo " OK\n";
$string = "'{$match[2]}'=>'" . $relative_path . "',\n"; $string = "'{$match[2]}'=>'" . $relative_path . "',\n";
$this->write($string); $this->write($string);
$classes[$match[2]] = $file->getRealPath(); $classes[$match[2]] = $file->getRealPath();
@ -64,7 +62,6 @@ class AutoloadBuilder
} }
} }
} }
echo "done.\n";
$this->closeAutoload(); $this->closeAutoload();
} }
@ -75,7 +72,7 @@ class AutoloadBuilder
protected function closeAutoload() protected function closeAutoload()
{ {
if ($this->write(");\n")) { if ($this->write(');')) {
fclose($this->handler); fclose($this->handler);
} }
} }
@ -84,11 +81,9 @@ class AutoloadBuilder
{ {
if (! $this->handler) { if (! $this->handler) {
if (! $this->handler = fopen($this->autoload, 'w')) { if (! $this->handler = fopen($this->autoload, 'w')) {
echo "{$this->autoload} is not writable\n"; trigger_error("{$this->autoload} is not writable", E_USER_ERROR);
die;
} }
} }
return (bool) fwrite($this->handler, $string); return (bool) fwrite($this->handler, $string);
} }
} }