Browse Source

Code refactoring, #16

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@115 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
master
pzinovkin 15 years ago
parent
commit
e33f2a8536
  1. 43
      Load.php
  2. 39
      app/FrontController.php
  3. 23
      exception/Error404Exception.php
  4. 29
      exception/GeneralException.php
  5. 15
      util/AutoloadBuilder.php

43
Load.php

@ -12,26 +12,53 @@
class Load class Load
{ {
static protected $file;
static protected $autoload; 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) 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);
}
} }

39
app/FrontController.php

@ -104,24 +104,37 @@ class FrontController
return $this->router; return $this->router;
} }
public function execute() public function execute()
{ {
try { 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) { } 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

@ -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

@ -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();
}
}

15
util/AutoloadBuilder.php

@ -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";
die;
trigger_error("{$this->autoload} is not writable", E_USER_ERROR);
} }
} }
return (bool) fwrite($this->handler, $string); return (bool) fwrite($this->handler, $string);
} }
} }
Loading…
Cancel
Save