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:
47
Load.php
47
Load.php
@ -12,26 +12,53 @@
|
||||
class Load
|
||||
{
|
||||
|
||||
static protected $file;
|
||||
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)
|
||||
{
|
||||
if (! isset(self::$autoload[$class])) {
|
||||
throw new MJException('There is no such class "' . $class . '" in autoload.');
|
||||
if (isset(self::$autoload[$class])) {
|
||||
require(PATH . self::$autoload[$class]);
|
||||
return;
|
||||
}
|
||||
require(PATH . self::$autoload[$class]);
|
||||
}
|
||||
|
||||
static public function setAutoloadFrom($file)
|
||||
{
|
||||
if (! file_exists($file)) {
|
||||
throw new MJException('Autoload source doesn\'t exists! Try to generate it!');
|
||||
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);
|
||||
}
|
||||
}
|
@ -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());
|
||||
} */
|
||||
}
|
||||
}
|
||||
}
|
23
exception/Error404Exception.php
Normal file
23
exception/Error404Exception.php
Normal 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');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
29
exception/GeneralException.php
Normal file
29
exception/GeneralException.php
Normal 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();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user