Permalink
Browse files

Introduced static Oxygen::stack, o(), o(/) for template component cla…

…ss management
  • Loading branch information...
1 parent ceb1153 commit 9741349cb1893bb7eb16345363bf9cda311aecd6 @Artazor Artazor committed May 14, 2012
View
41 asset/asset.class.php
@@ -11,6 +11,7 @@
public $cache = false;
private $loader = false;
+ private $manager = null;
const URL_REGEX = "|^https?://|";
const REMOTE_KEY_TEMPLATE = 'cached-url-{0}';
@@ -27,14 +28,16 @@ public function __construct($ext) {
public function __complete() {
$this->cache = $this->scope->cache;
$this->loader = $this->scope->loader;
+ $this->manager = $this->scope->assets;
}
public function addRemote($url) {
if(isset($this->list[$url])) return;
+ $assets = array();
+ $assets[$this->ext] = array(self::REMOTE_RESOURCE, $url);
$this->list[$url] = (object)array(
- 'type' => self::REMOTE_RESOURCE,
- 'path' => $url,
+ 'assets' => $assets,
'component' => false,
'class' => false,
'name' => false
@@ -49,10 +52,11 @@ public function getHashCode() {
$toHash = array();
$list = $this->list;
ksort($list);
- foreach($list as $key=>$asset) {
+ foreach($list as $key=>$item) {
$toHash[] = $key;
- if($asset->type == self::LOCAL_RESOURCE) {
- $toHash[] = filemtime($asset->path);
+ list($type, $path) = $item->assets[$this->ext];
+ if($type == self::LOCAL_RESOURCE) {
+ $toHash[] = filemtime($path);
}
}
$str = implode(':', $toHash);
@@ -71,11 +75,12 @@ protected function getCachedUrlContent($url) {
}
}
- protected function processOne($asset) {
- if($asset->type === self::REMOTE_RESOURCE){
- return $this->getCachedUrlContent($asset->path);
+ protected function processOne($item) {
+ list($type, $path) = $item->assets[$this->ext];
+ if($type === self::REMOTE_RESOURCE){
+ return $this->getCachedUrlContent($path);
} else {
- return file_get_contents($asset->path);
+ return file_get_contents($path);
}
}
@@ -110,21 +115,17 @@ protected function invalidate() {
}
public function add($call, $key = false) {
- list($class, $name, $component) = $call;
- if($key === false) $key = implode('::', $call);
+ if ($key === false) {
+ $key = $this->manager->getKey($call);
+ }
if(isset($this->added[$key])) return;
$this->added[$key] = true;
- $path = $this->loader->pathFor($class, $name . $this->ext);
+ $path = $this->loader->pathFor($call->class, $call->name . $this->ext);
if($path !== false) {
- $key = $path . '::' . $component;
+ $key = $path . '::' . $call->component;
if (!isset($this->list[$key])){
- $this->list[$key] = (object)array(
- 'type' => self::LOCAL_RESOURCE,
- 'path' => $path,
- 'class' => $class,
- 'name' => $name,
- 'component' => $component
- );
+ $call->assets[$this->ext] = array(self::LOCAL_RESOURCE, $path);
+ $this->list[$key] = $call;
$this->invalidate();
}
}
View
7 asset/js/js.class.php
@@ -14,9 +14,10 @@ class Oxygen_Asset_JS extends Oxygen_Asset {
public function __construct() {
parent::__construct('.js');
}
- protected function processOne($asset) {
- $source = parent::processOne($asset);
- if($asset->type === self::REMOTE_RESOURCE){
+ protected function processOne($item) {
+ $source = parent::processOne($item);
+ list($type, $path) = $item->assets[$this->ext];
+ if($type === self::REMOTE_RESOURCE){
return $source;
} else {
return Oxygen_Utils_Text::format(
View
6 asset/manager/manager.class.php
@@ -53,9 +53,13 @@ public function handled($path) {
};
return true;
}
+
+ public function getKey($call) {
+ return $call->class . '::' . $call->name . '::' . $call->component;
+ }
public function add($call) {
- $key = implode('::', $call);
+ $key = $this->getKey($call);
if (isset($this->added[$key])) return;
$this->added[$key] = true;
foreach ($this->assets as $asset) {
View
6 common/application/view.php
@@ -1,10 +1,10 @@
-<div class="<?=$this->getTemplateClass()?>">
+<?o()?>
<div class="oxy-wrap">
<?$this->put_header()?>
<?$this->put_menu()?>
<?$this->put_content()?>
</div>
-</div>
-<?$this->put_footer()?>
+<?o('/')?>
+<?$this->put_footer()?>
View
63 exception/exception.class.php
@@ -22,6 +22,7 @@ public function getName() {
// TODO: in php 5.4 this should be refactored with traits
// begin Copy-Paste block:
+
const OBJECT_CLASS = 'Oxygen_Object';
const EXCEPTION_CLASS = 'Oxygen_Exception';
const SCOPE_CLASS = 'Oxygen_Scope';
@@ -32,14 +33,12 @@ public function getName() {
const CALL_REGEXP = '/^(parent_)?(get_|put_|throw_|new_)(.*)$/';
const UNKNOWN_METHOD = 'Unknown method {0}->{1}';
- const CLAZZ = 0;
- const RESOURCE = 1;
- const COMPONENT = 2;
-
public $scope = null;
- private $stack = array();
public function __call($method, $args) {
+ if($method=='download'){
+ echo 'HERE';
+ }
$this->__assert(
preg_match(self::CALL_REGEXP, $method, $match),
self::UNKNOWN_METHOD,
@@ -48,11 +47,14 @@ public function __call($method, $args) {
);
$class = get_class($this);
if ($match[1] !== '') $class = get_parent_class($this);
+ if(!is_string($match[2])){
+ echo 'HERE';
+ }
return $this->{$match[2]}($match[3],$args);
}
public final function new_($class, $args = array()) {
- return $this->scope->resolve($class)->getInstance($args);
+ return $this->scope->resolve($class)->getInstance($args, $this->scope);
}
public final function throw_($class, $args) {
@@ -77,48 +79,42 @@ public function __call($method, $args) {
public final function put_($name, $args = array(), $class = false) {
$class = ($class === false) ? get_class($this) : $class;
- $call = array($class, $name, false);
+ $call = (object)array(
+ 'instance' => $this,
+ 'class' => $class,
+ 'name' => $name,
+ 'stack' => array(),
+ 'sp' => 0,
+ 'component' => false,
+ 'assets' => array()
+ );
+ Oxygen::push($call);
$scope = $this->scope;
$assets = $scope->assets;
- array_push($this->stack, $call);
+ $resource = (strpos($name,'.') === false)
+ ? $name . Oxygen_Loader::TEMPLATE_EXTENSION
+ : $name
+ ;
try {
include $scope->loader->pathFor(
$class,
- $name . Oxygen_Loader::TEMPLATE_EXTENSION
+ $resource
);
$ex = null;
} catch(Exception $e){
$ex = $e;
}
- if ($ex !== null) {
- array_pop($this->stack);
- throw $ex;
- } else {
- $assets->add(array_pop($this->stack));
- }
+ Oxygen::closeAll();
+ $result = Oxygen::pop();
+ if ($ex !== null) throw $ex;
+ $assets->add($result);
}
- public static function templateClassFor($class, $resource) {
- return 'css-' . $class . '-' . $resource;
+ public function getTemplateClass() {
+ return Oxygen::getCssClass();
}
- public final function getTemplateClass() {
- if(($count = count($this->stack)) == 0) {
- $this->throwException('getTemplateClass() call is valid only within template code');
- } else {
- $call = &$this->stack[$count-1];
- if($call[self::COMPONENT] === false) {
- return $call[self::COMPONENT] = self::templateClassFor(
- $call[self::CLAZZ],
- $call[self::RESOURCE]
- );
- } else {
- return $call[self::COMPONENT];
- }
- }
- }
-
public function __toString() {
return Oxygen_Utils_Text::format(self::DEFAULT_TO_STRING, get_class($this));
}
@@ -167,6 +163,7 @@ public static function isOxygenClass($class) {
);
}
+
// end Copy-Paste block.
View
8 itself.php
@@ -12,6 +12,14 @@
define('OXYGEN_XML_RESPONSE',4);
define('OXYGEN_REDIRECT_RESPONSE',5);
+ function o($tag = 'div') {
+ if($tag{0}=='/'){
+ Oxygen::close();
+ } else {
+ Oxygen::open($tag);
+ }
+ }
+
function jsonResponse($data, $headers = array()) {
return array(
'header' => 'Content-Type: application/json; Charset=UTF-8',
View
51 object/object.class.php
@@ -12,12 +12,7 @@ class Oxygen_Object {
const CALL_REGEXP = '/^(parent_)?(get_|put_|throw_|new_)(.*)$/';
const UNKNOWN_METHOD = 'Unknown method {0}->{1}';
- const CLAZZ = 0;
- const RESOURCE = 1;
- const COMPONENT = 2;
-
public $scope = null;
- private $stack = array();
public function __call($method, $args) {
if($method=='download'){
@@ -33,7 +28,7 @@ public function __call($method, $args) {
if ($match[1] !== '') $class = get_parent_class($this);
if(!is_string($match[2])){
echo 'HERE';
- }
+ }
return $this->{$match[2]}($match[3],$args);
}
@@ -63,11 +58,19 @@ public function __call($method, $args) {
public final function put_($name, $args = array(), $class = false) {
$class = ($class === false) ? get_class($this) : $class;
- $call = array($class, $name, false);
+ $call = (object)array(
+ 'instance' => $this,
+ 'class' => $class,
+ 'name' => $name,
+ 'stack' => array(),
+ 'sp' => 0,
+ 'component' => false,
+ 'assets' => array()
+ );
+ Oxygen::push($call);
$scope = $this->scope;
$assets = $scope->assets;
- array_push($this->stack, $call);
- $resource = (strpos($name,'.') === false)
+ $resource = (strpos($name,'.') === false)
? $name . Oxygen_Loader::TEMPLATE_EXTENSION
: $name
;
@@ -80,35 +83,17 @@ public function __call($method, $args) {
} catch(Exception $e){
$ex = $e;
}
- if ($ex !== null) {
- array_pop($this->stack);
- throw $ex;
- } else {
- $assets->add(array_pop($this->stack));
- }
+ Oxygen::closeAll();
+ $result = Oxygen::pop();
+ if ($ex !== null) throw $ex;
+ $assets->add($result);
}
- public static function templateClassFor($class,$resource) {
- return 'css-' . $class . '-' . $resource;
+ public function getTemplateClass() {
+ return Oxygen::getCssClass();
}
- public final function getTemplateClass() {
- if(($count = count($this->stack)) == 0) {
- $this->throwException('getTemplateClass() call is valid only within template code');
- } else {
- $call = &$this->stack[$count-1];
- if($call[self::COMPONENT] === false) {
- return $call[self::COMPONENT] = self::templateClassFor(
- $call[self::CLAZZ],
- $call[self::RESOURCE]
- );
- } else {
- return $call[self::COMPONENT];
- }
- }
- }
-
public function __toString() {
return Oxygen_Utils_Text::format(self::DEFAULT_TO_STRING, get_class($this));
}
View
51 oxygen.class.php
@@ -1,5 +1,56 @@
<?
class Oxygen {
+
+ private static $stack = array();
+ private static $sp = 0;
+
+ public static function push($call){
+ self::$stack[self::$sp++] = $call;
+ }
+
+ public static function pop() {
+ return self::$stack[--self::$sp];
+ }
+
+ public static function open($tag = 'div'){
+ $call = self::$stack[self::$sp-1];
+ $call->stack[$call->sp++] = $tag;
+ echo '<' . $tag . ' class="' . self::getCssClass() . '">';
+ }
+
+ public static function cssClassFor($class, $name) {
+ return 'css-' . $class . '-' . $name;
+ }
+
+ public static function close() {
+ $call = self::$stack[self::$sp-1];
+ $tag = $call->stack[--$call->sp];
+ echo '</' . $tag . '>';
+ }
+
+ public static function closeAll() {
+ $call = self::$stack[self::$sp-1];
+ while ($call->sp > 0) {
+ $tag = $call->stack[--$call->sp];
+ echo '</' . $tag . '>';
+ }
+ }
+
+ public static function getCssClass() {
+ if(self::$sp === 0) throw new Exception(
+ 'getCssClass() call is valid only within template code'
+ );
+ $call = self::$stack[self::$sp-1];
+ if ($call->component === false) {
+ return $call->component = self::cssClassFor(
+ $call->class,
+ $call->name
+ );
+ } else {
+ return $call->component;
+ }
+ }
+
public static function Generate() {
$scope = Oxygen_Scope::root();
$generator = $scope->Oxygen_Generator();

0 comments on commit 9741349

Please sign in to comment.