Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Greatly simplified metaprogramming part

  • Loading branch information...
commit bcd0321666b42af2038f6c6896cfde45a500a3f1 1 parent 248be29
Anatoly Ressin Artazor authored
Showing with 105 additions and 133 deletions.
  1. +35 −16 class/class.class.php
  2. +15 −24 oxy.php
  3. +43 −61 oxygen.class.php
  4. +12 −32 oxygen.oxy.php
51 class/class.class.php
View
@@ -3,6 +3,7 @@
define('OXYGEN_METACLASS_MODIFIED', filemtime(__FILE__));
class Oxygen_Class {
+
private $ref = null;
private $name = '';
private $less = null;
@@ -24,35 +25,53 @@ public function __toString() {
}
public function __getPublicInstanceMethod($name) {
- $m = $this->ref->getMethod($name);
+ $m = $this->ref->getMethod($name);
if ($m->isStatic()) throw new ReflectionException("$name is static");
if (!$m->isPublic()) throw new ReflectionException("$name is not public");
return $m;
}
- public function compile($source, $destination, $css, $name, $type, $time) {
- $include = dirname(__FILE__) . DIRECTORY_SEPARATOR . $type . '.php';
+ public function compile($asset) {
+
+ $type = $asset['type'];
+ $name = $asset['name'];
+ $ext = $asset['ext'];
+
+ $include = dirname(__FILE__)
+ . DIRECTORY_SEPARATOR
+ . $type
+ . '.php'
+ ;
+ $destination = OXYGEN_ASSET_ROOT
+ . DIRECTORY_SEPARATOR
+ . $type
+ . $this->__oxygen_path
+ . DIRECTORY_SEPARATOR
+ . $name
+ . $ext
+ ;
+ $source = OXYGEN_ROOT
+ . DIRECTORY_SEPARATOR
+ . $asset['path']
+ . DIRECTORY_SEPARATOR
+ . $name
+ . $ext
+ ;
try {
$d = filemtime($destination);
$s = filemtime($source);
$i = filemtime($include);
+ $t = $this->__lastMetaModified;
$m = OXYGEN_METACLASS_MODIFIED;
- if ($d >= max($s, $i, $time, $m)) return $d;
+ if ($d >= max($s, $i, $t, $m)) return array($d, $destination);
} catch (Oxygen_FileNotFoundException $e) {
Oxygen::getWritableDir(dirname($destination));
}
- $parent = $this->__getParentClass();
- if ($parent !== null) {
- try {
- $parent = 'css-'. $parent->{'__defines_'.$type.'_'.$name} . '-' . $name;
- } catch (ReflectionException $e) {
- $parent = $css;
- }
- } else {
- $parent = $css;
- }
+ $css = 'css-' . $this . '-' . $name;
try {
- unset($d);
+ unset($m);
+ unset($t);
+ unset($d);
unset($s);
unset($i);
ob_start();
@@ -63,7 +82,7 @@ public function compile($source, $destination, $css, $name, $type, $time) {
$result = '/* ' . $e->getMessage() . ' */';
}
file_put_contents($destination, $result);
- return time();
+ return array(time(), $destination);
}
}
39 oxy.php
View
@@ -7,6 +7,9 @@
class <?=$class->oxyName?> <?if($class->extends):?>extends <?=$class->extends?><?endif?> {
+ public static $__oxygen_path = '<?=$class->path?>';
+ public static $__lastMetaModified = <?=time()?>;
+
private static $static = null;
public static $name = '<?=$class->name?>';
public static function __getClass() {
@@ -21,7 +24,7 @@ public static function __getParentClass() {
return <?=$class->extends?>::__getClass();
<?else:?>
return null;
-<?endif?>
+<?endif?>
}
# SCOPE:
@@ -42,20 +45,20 @@ public static function __getParentClass() {
<?endforeach?>
<?foreach($class->assets as $asset):?>
- <?=$asset->access?> function asset_<?=$asset->name?>_<?=$asset->type?>($path, $css, $class, $last) {<?
- if(!$asset->override):?>}<?else:?>
+ <?=$asset->access?> function <?=$asset->method?>($class) {<?
+ if($asset->file === false):?>}<?else:?>
if(!isset($this->__assets)) {
$this->__assets = &$this->scope->assets;
}
- $name = $css . '-<?=$asset->name?>';
+ $name = $class . '-<?=$asset->name?>';
if (!isset($this->__assets['<?=$asset->type?>'][$name])) {
$this->__assets['<?=$asset->type?>'][$name] = array(
- 'source' => OXYGEN_ROOT . '<?=addslashes($asset->relPath)?>',
- 'destination' => OXYGEN_ASSET_ROOT . '/<?=$asset->type?>' . $path . '/<?=$asset->baseName?>',
- 'class' => $class,
- 'name' => '<?=$asset->name?>',
- 'last' => $last
+ 'type' => '<?=$asset->type?>',
+ 'ext' => '<?=$asset->ext?>',
+ 'name' => '<?=$asset->name?>',
+ 'path' => self::$__oxygen_path,
+ 'class' => $class
);
}
}
@@ -93,21 +96,12 @@ public static function __getParentClass() {
<?=$method->access?> function put_<?=$name?>(<?=$args?>) {
try {
Oxygen::push($this,'<?=$name?>');
- $result = include OXYGEN_ROOT . '<?=$method->relPath?>';
+ $result = include OXYGEN_ROOT . '<?=$class->path?>/<?=$name?>.php';
Oxygen::closeAll();
-<?if(count($method->assets)):?>
$class = $this->__getClass();
- $last = $this->__lastMetaModified();
- $css = 'css-' . $class;
<?foreach($method->assets as $asset):?>
- $this->asset_<?=$asset->name?>_<?=$asset->type?>(
- '<?=$method->path?>',
- $css,
- $class,
- $last
- );
-<?endforeach?>
-<?endif?>
+ $this-><?=$asset->method?>($class);
+<?endforeach?>
} catch (Exception $e) {
Oxygen::pop();
throw $e;
@@ -119,9 +113,6 @@ public static function __getParentClass() {
# END VIEWS.
- public function __lastMetaModified() {
- return <?=time()?>;
- }
}
<?if($class->both):?>class <?=$class->name?> extends <?$class->oxyName?> {
104 oxygen.class.php
View
@@ -127,21 +127,21 @@ public static function requireFile($relative) {
return require_once self::pathFor($relative);
}
- public function compileAsset($type, $source, $filename) {
+ public function compileAsset($type, $source, $fileName) {
switch ($type) {
case 'css':
case 'less':
require_once self::pathFor('oxygen/lib/lessphp/lessc.inc.php');
$less = new lessc();
try {
- file_put_contents($filename, $less->parse($source));
+ file_put_contents($fileName, $less->parse($source));
} catch (Exception $e) {
- file_put_contents($filename, '/* ' . $e->getMessage() . ' */');
+ file_put_contents($fileName, '/* ' . $e->getMessage() . ' */');
}
break;
case 'js':
default:
- file_put_contents($filename, $source);
+ file_put_contents($fileName, $source);
break;
}
@@ -150,21 +150,14 @@ public function compileAsset($type, $source, $filename) {
public function compileAssets() {
$result = array();
foreach ($this->scope->assets as $type => $assets) {
- $names = array();
+ $fileNames = array();
$last = 1;
- foreach($assets as $css => $asset) {
- $time = $asset['class']->compile(
- $asset['source'],
- $asset['destination'],
- $css,
- $asset['name'],
- $type,
- $asset['last']
- );
+ foreach($assets as $asset) {
+ list($time, $fileName) = $asset['class']->compile($asset);
$last = max($last, $time);
- $names[] = $asset['destination'];
+ $fileNames[] = $fileName;
}
- $bundle = md5(implode(':',$names));
+ $bundle = md5(implode(':',$fileNames));
$bundle_path = OXYGEN_ASSET_ROOT
. DIRECTORY_SEPARATOR . $type
. DIRECTORY_SEPARATOR . $bundle
@@ -172,10 +165,11 @@ public function compileAssets() {
$m = self::modificationTime($bundle_path);
if ($last > $m) {
ob_start();
- foreach($names as $source) {
- $s = substr($source,strlen(OXYGEN_ASSET_ROOT)+1);
+ foreach($fileNames as $fileName) {
+ $s = substr($fileName, strlen(OXYGEN_ASSET_ROOT)+1);
echo '/* ' . $s . " */\n";
- readfile($source);
+ readfile($fileName);
+ echo "\n";
}
$this->compileAsset($type, ob_get_clean(), $bundle_path);
}
@@ -266,6 +260,7 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
$pattern = '/(?:' . implode('|', array_map('preg_quote', array_keys($all))) . ')$/';
foreach ($files as $file) {
$name = basename($file);
+ $file = '/' . $path . '/' . $file;
if (preg_match($pattern, $name, $m)) {
$ext = $m[0];
$name = substr($name, 0, -strlen($ext));
@@ -291,6 +286,7 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
? $yaml['views']
: array()
;
+
$views = array();
foreach ($all['.php'] as $name => $file) {
$defaultAccess = 'private';
@@ -309,9 +305,6 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
$yamlView = array();
}
$views[$name] = (object)array(
- 'relPath' => str_replace(DIRECTORY_SEPARATOR,'/',$path . DIRECTORY_SEPARATOR . basename($file)),
- 'absPath' => str_replace(DIRECTORY_SEPARATOR,'/',$file),
- 'path' => str_replace(DIRECTORY_SEPARATOR,'/',$path),
'access' => isset($yamlView['access']) ? $yamlView['access'] : $defaultAccess,
'args' => isset($yamlView['args']) ? $yamlView['args'] : array(),
'info' => isset($yamlView['info']) ? $yamlView['info'] : ($name . ' view'),
@@ -330,39 +323,34 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
foreach ($views as $name => &$view) {
foreach ($assetExt as $type => $ext) {
$method = 'asset_' . $name . '_' . $type;
- if (isset($all[$ext][$name])) {
- $file = $all[$ext][$name];
- $assets[] = $view->assets[$method] = (object)array(
- 'override' => true,
- 'name' => $name,
- 'relPath' => str_replace(DIRECTORY_SEPARATOR,'/',$path . DIRECTORY_SEPARATOR . basename($file)),
- 'absPath' => $file,
- 'baseName' => basename($file),
- 'type' => $type,
- 'genPath' => OXYGEN_ASSET_ROOT . DIRECTORY_SEPARATOR
- . $type . $path . DIRECTORY_SEPARATOR
- . basename($file),
- 'access' => $view->access
- );
+ $file = isset($all[$ext][$name])
+ ? $all[$ext][$name]
+ : false
+ ;
+ $asset = (object)array(
+ 'name' => $name,
+ 'file' => $file,
+ 'ext' => $ext,
+ 'type' => $type,
+ 'method' => $method,
+ 'access' => $view->access
+ );
+ if ($file !== false) {
+ // asset is defined in current class
+ // display it both in defs and usages
+ $assets[] = $view->assets[$method] = $asset;
} else if ($ancestor_access[$name] === 'private') {
+ // asset is not inherited and file is not present;
if ($view->access !== 'private') {
- $assets[] = $view->assets[$method] = (object)array(
- 'override' => false,
- 'name' => $name,
- 'type' => $type,
- 'method' => $method,
- 'access' => $view->access
- );
+ //asset should be introduced since view is inheritable
+ //we should create an empty entries in defs
+ //and also generate usage;
+ $assets[] = $view->assets[$method] = $asset;
}
} else {
- $view->assets[$method] = (object)array(
- 'override' => false,
- 'name' => $name,
- 'type' => $type,
- 'method' => $method,
- 'baseName' => $name . $ext,
- 'access' => $view->access
- );
+ //asset is simply inherited, so we'll
+ //generate only it's usage
+ $view->assets[$method] = $asset;
}
}
}
@@ -381,18 +369,12 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
'scope' => $scope,
'oxyName' => $className . self::OXYGEN_SUFFIX,
'views' => $views,
- 'assets' => $assets
+ 'assets' => $assets,
+ 'path' => $path
);
try {
- $generated = $this->get_oxy($class);
- $f = fopen($genPath,'w+');
- if (flock($f,LOCK_EX)) {
- ftruncate($f, 0);
- fwrite($f, $generated);
- flock($f, LOCK_UN);
- }
- fclose($f);
+ file_put_contents($genPath, $this->get_oxy($class));
} catch (Exception $e) {
print $e;
}
@@ -428,7 +410,7 @@ public function loadClass($class) {
$compilePath,
$base ? substr($class,0,-strlen(self::OXYGEN_SUFFIX)) : $class,
$yaml,
- $path,
+ str_replace(DIRECTORY_SEPARATOR, '/', $path),
!$base
);
}
44 oxygen.oxy.php
View
@@ -6,6 +6,9 @@
class Oxygen_ {
+ public static $__oxygen_path = '/oxygen';
+ public static $__lastMetaModified = 1339287938;
+
private static $static = null;
public static $name = 'Oxygen';
public static function __getClass() {
@@ -17,7 +20,6 @@ public static function __getClass() {
public static function __getParentClass() {
return null;
-
}
# SCOPE:
@@ -34,9 +36,9 @@ public static function __getParentClass() {
public static $__defines_less_inspected = 'Oxygen';
public static $__defines_js_inspected = 'Oxygen';
- public function asset_inspected_css($path, $css, $class, $last) {}
- public function asset_inspected_less($path, $css, $class, $last) {}
- public function asset_inspected_js($path, $css, $class, $last) {}
+ public function asset_inspected_css($class) {}
+ public function asset_inspected_less($class) {}
+ public function asset_inspected_js($class) {}
# END ASSETS.
# BEGIN VIEWS:
@@ -59,7 +61,7 @@ private function put_exception($ex) {
Oxygen::push($this,'exception');
$result = include OXYGEN_ROOT . '/oxygen/exception.php';
Oxygen::closeAll();
-
+ $class = $this->__getClass();
} catch (Exception $e) {
Oxygen::pop();
throw $e;
@@ -86,7 +88,7 @@ private function put_exception_trace($trace) {
Oxygen::push($this,'exception_trace');
$result = include OXYGEN_ROOT . '/oxygen/exception_trace.php';
Oxygen::closeAll();
-
+ $class = $this->__getClass();
} catch (Exception $e) {
Oxygen::pop();
throw $e;
@@ -114,28 +116,9 @@ public function put_inspected($value) {
$result = include OXYGEN_ROOT . '/oxygen/inspected.php';
Oxygen::closeAll();
$class = $this->__getClass();
- $last = $this->__lastMetaModified();
- $css = 'css-' . $class;
- $this->asset_inspected_css(
- '/oxygen',
- $css,
- $class,
- $last
- );
- $this->asset_inspected_less(
- '/oxygen',
- $css,
- $class,
- $last
- );
- $this->asset_inspected_js(
- '/oxygen',
- $css,
- $class,
- $last
- );
-
-
+ $this->asset_inspected_css($class);
+ $this->asset_inspected_less($class);
+ $this->asset_inspected_js($class);
} catch (Exception $e) {
Oxygen::pop();
throw $e;
@@ -162,7 +145,7 @@ private function put_oxy($class) {
Oxygen::push($this,'oxy');
$result = include OXYGEN_ROOT . '/oxygen/oxy.php';
Oxygen::closeAll();
-
+ $class = $this->__getClass();
} catch (Exception $e) {
Oxygen::pop();
throw $e;
@@ -173,9 +156,6 @@ private function put_oxy($class) {
# END VIEWS.
- public function __lastMetaModified() {
- return 1339276099;
- }
}
Please sign in to comment.
Something went wrong with that request. Please try again.