Skip to content

Commit

Permalink
Greatly simplified metaprogramming part
Browse files Browse the repository at this point in the history
  • Loading branch information
Artazor committed Jun 10, 2012
1 parent 248be29 commit bcd0321
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 133 deletions.
51 changes: 35 additions & 16 deletions class/class.class.php
Expand Up @@ -3,6 +3,7 @@
define('OXYGEN_METACLASS_MODIFIED', filemtime(__FILE__)); define('OXYGEN_METACLASS_MODIFIED', filemtime(__FILE__));


class Oxygen_Class { class Oxygen_Class {

private $ref = null; private $ref = null;
private $name = ''; private $name = '';
private $less = null; private $less = null;
Expand All @@ -24,35 +25,53 @@ public function __toString() {
} }


public function __getPublicInstanceMethod($name) { 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->isStatic()) throw new ReflectionException("$name is static");
if (!$m->isPublic()) throw new ReflectionException("$name is not public"); if (!$m->isPublic()) throw new ReflectionException("$name is not public");
return $m; return $m;
} }


public function compile($source, $destination, $css, $name, $type, $time) { public function compile($asset) {
$include = dirname(__FILE__) . DIRECTORY_SEPARATOR . $type . '.php';
$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 { try {
$d = filemtime($destination); $d = filemtime($destination);
$s = filemtime($source); $s = filemtime($source);
$i = filemtime($include); $i = filemtime($include);
$t = $this->__lastMetaModified;
$m = OXYGEN_METACLASS_MODIFIED; $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) { } catch (Oxygen_FileNotFoundException $e) {
Oxygen::getWritableDir(dirname($destination)); Oxygen::getWritableDir(dirname($destination));
} }
$parent = $this->__getParentClass(); $css = 'css-' . $this . '-' . $name;
if ($parent !== null) {
try {
$parent = 'css-'. $parent->{'__defines_'.$type.'_'.$name} . '-' . $name;
} catch (ReflectionException $e) {
$parent = $css;
}
} else {
$parent = $css;
}
try { try {
unset($d); unset($m);
unset($t);
unset($d);
unset($s); unset($s);
unset($i); unset($i);
ob_start(); ob_start();
Expand All @@ -63,7 +82,7 @@ public function compile($source, $destination, $css, $name, $type, $time) {
$result = '/* ' . $e->getMessage() . ' */'; $result = '/* ' . $e->getMessage() . ' */';
} }
file_put_contents($destination, $result); file_put_contents($destination, $result);
return time(); return array(time(), $destination);
} }
} }


Expand Down
39 changes: 15 additions & 24 deletions oxy.php
Expand Up @@ -7,6 +7,9 @@


class <?=$class->oxyName?> <?if($class->extends):?>extends <?=$class->extends?><?endif?> { class <?=$class->oxyName?> <?if($class->extends):?>extends <?=$class->extends?><?endif?> {


public static $__oxygen_path = '<?=$class->path?>';
public static $__lastMetaModified = <?=time()?>;

private static $static = null; private static $static = null;
public static $name = '<?=$class->name?>'; public static $name = '<?=$class->name?>';
public static function __getClass() { public static function __getClass() {
Expand All @@ -21,7 +24,7 @@ public static function __getParentClass() {
return <?=$class->extends?>::__getClass(); return <?=$class->extends?>::__getClass();
<?else:?> <?else:?>
return null; return null;
<?endif?> <?endif?>
} }


# SCOPE: # SCOPE:
Expand All @@ -42,20 +45,20 @@ public static function __getParentClass() {
<?endforeach?> <?endforeach?>


<?foreach($class->assets as $asset):?> <?foreach($class->assets as $asset):?>
<?=$asset->access?> function asset_<?=$asset->name?>_<?=$asset->type?>($path, $css, $class, $last) {<? <?=$asset->access?> function <?=$asset->method?>($class) {<?
if(!$asset->override):?>}<?else:?> if($asset->file === false):?>}<?else:?>


if(!isset($this->__assets)) { if(!isset($this->__assets)) {
$this->__assets = &$this->scope->assets; $this->__assets = &$this->scope->assets;
} }
$name = $css . '-<?=$asset->name?>'; $name = $class . '-<?=$asset->name?>';
if (!isset($this->__assets['<?=$asset->type?>'][$name])) { if (!isset($this->__assets['<?=$asset->type?>'][$name])) {
$this->__assets['<?=$asset->type?>'][$name] = array( $this->__assets['<?=$asset->type?>'][$name] = array(
'source' => OXYGEN_ROOT . '<?=addslashes($asset->relPath)?>', 'type' => '<?=$asset->type?>',
'destination' => OXYGEN_ASSET_ROOT . '/<?=$asset->type?>' . $path . '/<?=$asset->baseName?>', 'ext' => '<?=$asset->ext?>',
'class' => $class, 'name' => '<?=$asset->name?>',
'name' => '<?=$asset->name?>', 'path' => self::$__oxygen_path,
'last' => $last 'class' => $class
); );
} }
} }
Expand Down Expand Up @@ -93,21 +96,12 @@ public static function __getParentClass() {
<?=$method->access?> function put_<?=$name?>(<?=$args?>) { <?=$method->access?> function put_<?=$name?>(<?=$args?>) {
try { try {
Oxygen::push($this,'<?=$name?>'); Oxygen::push($this,'<?=$name?>');
$result = include OXYGEN_ROOT . '<?=$method->relPath?>'; $result = include OXYGEN_ROOT . '<?=$class->path?>/<?=$name?>.php';
Oxygen::closeAll(); Oxygen::closeAll();
<?if(count($method->assets)):?>
$class = $this->__getClass(); $class = $this->__getClass();
$last = $this->__lastMetaModified();
$css = 'css-' . $class;
<?foreach($method->assets as $asset):?> <?foreach($method->assets as $asset):?>
$this->asset_<?=$asset->name?>_<?=$asset->type?>( $this-><?=$asset->method?>($class);
'<?=$method->path?>', <?endforeach?>
$css,
$class,
$last
);
<?endforeach?>
<?endif?>
} catch (Exception $e) { } catch (Exception $e) {
Oxygen::pop(); Oxygen::pop();
throw $e; throw $e;
Expand All @@ -119,9 +113,6 @@ public static function __getParentClass() {


# END VIEWS. # END VIEWS.


public function __lastMetaModified() {
return <?=time()?>;
}
} }


<?if($class->both):?>class <?=$class->name?> extends <?$class->oxyName?> { <?if($class->both):?>class <?=$class->name?> extends <?$class->oxyName?> {
Expand Down
104 changes: 43 additions & 61 deletions oxygen.class.php
Expand Up @@ -127,21 +127,21 @@ public static function requireFile($relative) {
return require_once self::pathFor($relative); return require_once self::pathFor($relative);
} }


public function compileAsset($type, $source, $filename) { public function compileAsset($type, $source, $fileName) {
switch ($type) { switch ($type) {
case 'css': case 'css':
case 'less': case 'less':
require_once self::pathFor('oxygen/lib/lessphp/lessc.inc.php'); require_once self::pathFor('oxygen/lib/lessphp/lessc.inc.php');
$less = new lessc(); $less = new lessc();
try { try {
file_put_contents($filename, $less->parse($source)); file_put_contents($fileName, $less->parse($source));
} catch (Exception $e) { } catch (Exception $e) {
file_put_contents($filename, '/* ' . $e->getMessage() . ' */'); file_put_contents($fileName, '/* ' . $e->getMessage() . ' */');
} }
break; break;
case 'js': case 'js':
default: default:
file_put_contents($filename, $source); file_put_contents($fileName, $source);
break; break;
} }


Expand All @@ -150,32 +150,26 @@ public function compileAsset($type, $source, $filename) {
public function compileAssets() { public function compileAssets() {
$result = array(); $result = array();
foreach ($this->scope->assets as $type => $assets) { foreach ($this->scope->assets as $type => $assets) {
$names = array(); $fileNames = array();
$last = 1; $last = 1;
foreach($assets as $css => $asset) { foreach($assets as $asset) {
$time = $asset['class']->compile( list($time, $fileName) = $asset['class']->compile($asset);
$asset['source'],
$asset['destination'],
$css,
$asset['name'],
$type,
$asset['last']
);
$last = max($last, $time); $last = max($last, $time);
$names[] = $asset['destination']; $fileNames[] = $fileName;
} }
$bundle = md5(implode(':',$names)); $bundle = md5(implode(':',$fileNames));
$bundle_path = OXYGEN_ASSET_ROOT $bundle_path = OXYGEN_ASSET_ROOT
. DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $type
. DIRECTORY_SEPARATOR . $bundle . DIRECTORY_SEPARATOR . $bundle
. '.' . $type; . '.' . $type;
$m = self::modificationTime($bundle_path); $m = self::modificationTime($bundle_path);
if ($last > $m) { if ($last > $m) {
ob_start(); ob_start();
foreach($names as $source) { foreach($fileNames as $fileName) {
$s = substr($source,strlen(OXYGEN_ASSET_ROOT)+1); $s = substr($fileName, strlen(OXYGEN_ASSET_ROOT)+1);
echo '/* ' . $s . " */\n"; echo '/* ' . $s . " */\n";
readfile($source); readfile($fileName);
echo "\n";
} }
$this->compileAsset($type, ob_get_clean(), $bundle_path); $this->compileAsset($type, ob_get_clean(), $bundle_path);
} }
Expand Down Expand Up @@ -266,6 +260,7 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
$pattern = '/(?:' . implode('|', array_map('preg_quote', array_keys($all))) . ')$/'; $pattern = '/(?:' . implode('|', array_map('preg_quote', array_keys($all))) . ')$/';
foreach ($files as $file) { foreach ($files as $file) {
$name = basename($file); $name = basename($file);
$file = '/' . $path . '/' . $file;
if (preg_match($pattern, $name, $m)) { if (preg_match($pattern, $name, $m)) {
$ext = $m[0]; $ext = $m[0];
$name = substr($name, 0, -strlen($ext)); $name = substr($name, 0, -strlen($ext));
Expand All @@ -291,6 +286,7 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
? $yaml['views'] ? $yaml['views']
: array() : array()
; ;

$views = array(); $views = array();
foreach ($all['.php'] as $name => $file) { foreach ($all['.php'] as $name => $file) {
$defaultAccess = 'private'; $defaultAccess = 'private';
Expand All @@ -309,9 +305,6 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
$yamlView = array(); $yamlView = array();
} }
$views[$name] = (object)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, 'access' => isset($yamlView['access']) ? $yamlView['access'] : $defaultAccess,
'args' => isset($yamlView['args']) ? $yamlView['args'] : array(), 'args' => isset($yamlView['args']) ? $yamlView['args'] : array(),
'info' => isset($yamlView['info']) ? $yamlView['info'] : ($name . ' view'), 'info' => isset($yamlView['info']) ? $yamlView['info'] : ($name . ' view'),
Expand All @@ -330,39 +323,34 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
foreach ($views as $name => &$view) { foreach ($views as $name => &$view) {
foreach ($assetExt as $type => $ext) { foreach ($assetExt as $type => $ext) {
$method = 'asset_' . $name . '_' . $type; $method = 'asset_' . $name . '_' . $type;
if (isset($all[$ext][$name])) { $file = isset($all[$ext][$name])
$file = $all[$ext][$name]; ? $all[$ext][$name]
$assets[] = $view->assets[$method] = (object)array( : false
'override' => true, ;
'name' => $name, $asset = (object)array(
'relPath' => str_replace(DIRECTORY_SEPARATOR,'/',$path . DIRECTORY_SEPARATOR . basename($file)), 'name' => $name,
'absPath' => $file, 'file' => $file,
'baseName' => basename($file), 'ext' => $ext,
'type' => $type, 'type' => $type,
'genPath' => OXYGEN_ASSET_ROOT . DIRECTORY_SEPARATOR 'method' => $method,
. $type . $path . DIRECTORY_SEPARATOR 'access' => $view->access
. basename($file), );
'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') { } else if ($ancestor_access[$name] === 'private') {
// asset is not inherited and file is not present;
if ($view->access !== 'private') { if ($view->access !== 'private') {
$assets[] = $view->assets[$method] = (object)array( //asset should be introduced since view is inheritable
'override' => false, //we should create an empty entries in defs
'name' => $name, //and also generate usage;
'type' => $type, $assets[] = $view->assets[$method] = $asset;
'method' => $method,
'access' => $view->access
);
} }
} else { } else {
$view->assets[$method] = (object)array( //asset is simply inherited, so we'll
'override' => false, //generate only it's usage
'name' => $name, $view->assets[$method] = $asset;
'type' => $type,
'method' => $method,
'baseName' => $name . $ext,
'access' => $view->access
);
} }
} }
} }
Expand All @@ -381,18 +369,12 @@ private function compileItem($normalDir, $genPath, $className, $yaml, $path, $bo
'scope' => $scope, 'scope' => $scope,
'oxyName' => $className . self::OXYGEN_SUFFIX, 'oxyName' => $className . self::OXYGEN_SUFFIX,
'views' => $views, 'views' => $views,
'assets' => $assets 'assets' => $assets,
'path' => $path
); );


try { try {
$generated = $this->get_oxy($class); file_put_contents($genPath, $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);
} catch (Exception $e) { } catch (Exception $e) {
print $e; print $e;
} }
Expand Down Expand Up @@ -428,7 +410,7 @@ public function loadClass($class) {
$compilePath, $compilePath,
$base ? substr($class,0,-strlen(self::OXYGEN_SUFFIX)) : $class, $base ? substr($class,0,-strlen(self::OXYGEN_SUFFIX)) : $class,
$yaml, $yaml,
$path, str_replace(DIRECTORY_SEPARATOR, '/', $path),
!$base !$base
); );
} }
Expand Down

0 comments on commit bcd0321

Please sign in to comment.