[Wact-cvs] SF.net SVN: wact: [593] trunk/wact
Status: Pre-Alpha
Brought to you by:
jeffmoore
From: <Jef...@us...> - 2007-10-21 20:29:13
|
Revision: 593 http://wact.svn.sourceforge.net/wact/?rev=593&view=rev Author: JeffMoore Date: 2007-10-21 13:29:11 -0700 (Sun, 21 Oct 2007) Log Message: ----------- Split the concept of function into functions and methods to allow better use of polymorphism. Modified Paths: -------------- trunk/wact/compiler/codewriter/codewriter.inc.php trunk/wact/compiler/codewriter/codewriter.test.php trunk/wact/template/compiler/node/view.inc.php Modified: trunk/wact/compiler/codewriter/codewriter.inc.php =================================================================== --- trunk/wact/compiler/codewriter/codewriter.inc.php 2007-10-21 18:44:52 UTC (rev 592) +++ trunk/wact/compiler/codewriter/codewriter.inc.php 2007-10-21 20:29:11 UTC (rev 593) @@ -15,6 +15,11 @@ */ /** +*/ +interface WactCodeWriterTopLevelScope { +} + +/** * Provides an API for generating PHP include code blocks * @package compiler */ @@ -286,12 +291,58 @@ } /** +*/ +class WactGlobalCodeWriter extends WactBlockCodeWriter implements WactCodeWriterTopLevelScope { + + /** + * Undefined Operation + */ + public function createTempProperty() { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + + /** + * Undefined Operation + */ + public function registerProperty($property) { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + + /** + * Undefined Operation + */ + public function createProperty($name, $value = null, $visibility = 'public') { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + + /** + * Undefined Operation + */ + public function findProperty($name) { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + + /** + * Undefined Operation + */ + public function propertyExists($name) { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + +} + +/** * Provides an API for generating PHP functions. * This class starts out in PHP mode thus the code it returns * always assumes the PHP context (i.e. adds NO PHP tags internally). * @package compiler */ -class WactFunctionCodeWriter extends WactBlockCodeWriter { +class WactCallableCodeWriter extends WactBlockCodeWriter { /** * Name of the function * @var string @@ -347,6 +398,53 @@ } } +class WactFunctionCodeWriter extends WactCallableCodeWriter implements WactCodeWriterTopLevelScope { + + /** + * Undefined Operation + */ + public function createTempProperty() { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + + /** + * Undefined Operation + */ + public function registerProperty($property) { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + + /** + * Undefined Operation + */ + public function createProperty($name, $value = null, $visibility = 'public') { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + + /** + * Undefined Operation + */ + public function findProperty($name) { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + + /** + * Undefined Operation + */ + public function propertyExists($name) { + require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; + throw new WactInvalidScopeException(); + } + +} + +class WactMethodCodeWriter extends WactCallableCodeWriter { +} + /** * Provides an API for generating member variables for classes * @package compiler @@ -450,7 +548,7 @@ * always assumes PHP context (i.e. adds NO PHP tags internally). * @package compiler */ -class WactClassCodeWriter { +class WactClassCodeWriter implements WactCodeWriterTopLevelScope { /** * Name of the class * @var string @@ -482,10 +580,10 @@ protected $properties = array(); /** - * List of the registered functions - * @var array of WactFunctionCodeWriter + * List of the registered methods + * @var array of WactMethodCodeWriter */ - protected $functions = array(); + protected $methods = array(); /** * List of files to write include statements for @@ -495,7 +593,7 @@ /** * Internal pointer to the currently selected block - * @var WactFunctionCodeWriter + * @var WactMethodCodeWriter */ protected $scope = null; @@ -532,14 +630,14 @@ /** * Returns the include file list. - * Class includes are merged with the child functions includes in the result. + * Class includes are merged with the child method's includes in the result. * @return array */ public function getIncludes() { $includes = $this->includes->getIncludes(); - foreach ($this->functions as $function) { - $includes = array_merge($includes, $function->getIncludes()); + foreach ($this->methods as $method) { + $includes = array_merge($includes, $method->getIncludes()); } return $includes; @@ -557,7 +655,7 @@ /** * Sets another scope to be active. - * @param WactFunctionCodeWriter $scope The scope to set + * @param WactMethodCodeWriter $scope The scope to set * @return The scope that was active when calling this function * @see self::switchScope() */ @@ -571,28 +669,28 @@ /** * Sets another scope to be active. - * The function has to be registered using registerFunction() first. - * @param string $functionName Name of the function to select + * The method has to be registered using registerMethod() first. + * @param string $MethodName Name of the method to select * @return The scope that was active when calling this function * @see self::setScope() */ - public function switchScope($functionName = null) { + public function switchScope($methodName = null) { $oldScope = $this->scope; - if ($functionName == null) { + if ($methodName == null) { $this->scope = null; return $oldScope; } - // check that the function has been registered with this class - $function = $this->findFunction($functionName); - if (!$function) { + // check that the method has been registered with this class + $method = $this->findMethod($methodName); + if (!$method) { require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; - throw new WactFunctionNotFoundException($functionName); + throw new WactFunctionNotFoundException($methodName); } - $this->scope = $function; + $this->scope = $method; return $oldScope; } @@ -669,60 +767,60 @@ } /** - * Creates a new WactFunctionCodeWriter and registers it with this class. + * Creates a new WactMethodCodeWriter and registers it with this class. * Calling this function results in an implicit scope switch - * (sets the active scope to the newly created function). - * @param string $name Name of the function + * (sets the active scope to the newly created method). + * @param string $name Name of the method * @param string $params Parameter list (without the brackets) - * @return string Function name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. + * @return string method name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. */ - public function createFunction($name, $params = null) { - $function = new WactFunctionCodeWriter($name, $params); + public function createMethod($name, $params = null) { + $method = new WactMethodCodeWriter($name, $params); - return $this->registerFunction($function); + return $this->registerMethod($method); } /** - * Registers a function with this class. - * Checks that no function with this name has been registered yet. + * Registers a method with this class. + * Checks that no method with this name has been registered yet. * Calling this function results in an implicit scope switch. - * (sets the active scope to the newly registered function) - * @param WactFunctionCodeWriter $function - * @return string Function name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. + * (sets the active scope to the newly registered method) + * @param WactMethodCodeWriter $method + * @return string method name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. */ - public function registerFunction($function) { - $name = $function->getName(); - if ($this->functionExists($name)) { + public function registerMethod(WactMethodCodeWriter $method) { + $name = $method->getName(); + if ($this->methodExists($name)) { require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; throw new WactFunctionExistsException($name); } - $this->functions[$name] = $function; - $this->scope = $function; + $this->methods[$name] = $method; + $this->scope = $method; return $name; } /** - * Returns a function that previously has been registered with this class. - * @param string $name Name of the function to find - * @return WactFunctionCodeWriter or null + * Returns a method that previously has been registered with this class. + * @param string $name Name of the method to find + * @return WactMethodCodeWriter|NULL */ - public function findFunction($name) { - if (array_key_exists($name, $this->functions)) { - return $this->functions[$name]; + public function findMethod($name) { + if (array_key_exists($name, $this->methods)) { + return $this->methods[$name]; } return null; } /** - * Checks if a function with the specified name is registered with this class. + * Checks if a method with the specified name is registered with this class. * @param string $name The name to check * @return boolean */ - public function functionExists($name) { - return ($this->findFunction($name) != null); + public function methodExists($name) { + return ($this->findMethod($name) != null); } /** @@ -832,8 +930,8 @@ $code .= "\n" . $property->getDeclaration() . "\n"; } - foreach ($this->functions as $function) { - $code .= "\n" . $function->getBlockCode() . "\n"; + foreach ($this->methods as $method) { + $code .= "\n" . $method->getBlockCode() . "\n"; } $code .= "\n}\n"; @@ -882,13 +980,13 @@ /** * The global code block of the file - * @var WactBlockCodeWriter + * @var WactGlobalCodeWriter */ protected $globalBlock; /** * The currently active scope - * @var WactClassCodeWriter or WactFunctionCodeWriter or WactBlockCodeWriter + * @var WactCodeWriterTopLevelScope */ protected $scope; @@ -896,18 +994,18 @@ * Class constructor. */ public function __construct() { - $this->globalBlock = new WactBlockCodeWriter(WactBlockCodeWriter::MODE_HTML); - $this->scope = $this->globalBlock; + $this->globalBlock = new WactGlobalCodeWriter(WactBlockCodeWriter::MODE_HTML); + $this->setScope($this->globalBlock); } /** * Sets another scope to be active. * Switching to global scope is possible using the switchScope function. - * @param WactClassCodeWriter|WactFunctionCodeWriter|WactBlockCodeWriter $scope The scope to set + * @param WactCodeWriterTopLevelScope $scope The scope to set * @return The scope that was active when calling this function * @see self::switchScope() */ - public function setScope($scope) { + public function setScope(WactCodeWriterTopLevelScope $scope) { $oldScope = $this->scope; $this->scope = $scope; @@ -971,15 +1069,21 @@ * @return string The name of the property. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. */ public function createTempProperty() { - if (!($this->scope instanceof WactClassCodeWriter)) { - require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; - throw new WactInvalidScopeException(); - } - return $this->scope->createTempProperty(); } /** + * Registers a property with the current scope. + * The current scope must be a WactClassCodeWriter. + * Checks that no property with this name has been registered yet. + * @param WactPropertyCodeWriter $property + * @return string Property name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. + */ + public function registerProperty($property) { + return $this->scope->registerProperty($property); + } + + /** * Creates a new WactPropertyCodeWriter and registers it with the current scope. * The current scope must be a WactClassCodeWriter. * @param string $name Property name @@ -988,42 +1092,16 @@ * @return string Property name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. */ public function createProperty($name, $value = null, $visibility = 'public') { - if (!($this->scope instanceof WactClassCodeWriter)) { - require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; - throw new WactInvalidScopeException(); - } - return $this->scope->createProperty($name, $value, $visibility); } /** - * Registers a property with the current scope. - * The current scope must be a WactClassCodeWriter. - * Checks that no property with this name has been registered yet. - * @param WactPropertyCodeWriter $property - * @return string Property name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. - */ - public function registerProperty($property) { - if (!($this->scope instanceof WactClassCodeWriter)) { - require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; - throw new WactInvalidScopeException(); - } - - return $this->scope->registerProperty($property); - } - - /** * Returns a property that previously has been registered with the current scope. * The current scope must be a WactClassCodeWriter. * @param string $name Name of the property to find * @return WactPropertyCodeWriter or null */ public function findProperty($name) { - if (!($this->scope instanceof WactClassCodeWriter)) { - require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; - throw new WactInvalidScopeException(); - } - return $this->scope->findProperty($name); } @@ -1034,33 +1112,36 @@ * @return boolean */ public function propertyExists($name) { - if (!($this->scope instanceof WactClassCodeWriter)) { - require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; - throw new WactInvalidScopeException(); - } - return $this->scope->propertyExists($name); } /** - * Creates a new WactFunctionCodeWriter either in class or global scope. - * Calling this function results in an implicit scope switch + * Creates a new WactFunctionCodeWriter. * (sets the active scope to the newly created function). * @param string $name Name of the function * @param string $params Parameter list (without the brackets) * @return string Function name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. */ public function createFunction($name, $params = null) { - if ($this->scope instanceof WactClassCodeWriter) { - return $this->scope->createFunction($name, $params); - } - $function = new WactFunctionCodeWriter($name, $params); return $this->registerFunction($function); } /** - * Registers a function either in class scope or global scope. + * Creates a new WactMethodCodeWriter. + * Calling this function results in an implicit scope switch + * (sets the active scope to the newly created function). + * @param string $name Name of the function + * @param string $params Parameter list (without the brackets) + * @return string Function name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. + */ + public function createMethod($name, $params = null) { + return $this->scope->createMethod($name, $params); + } + + + /** + * Registers a function. * Checks that no function with this name has been registered yet. * Calling this function results in an implicit scope switch. * (sets the active scope to the newly registered function) @@ -1068,34 +1149,38 @@ * @throws WactFunctionExistsException * @return string Function name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. */ - public function registerFunction($function) { - if ($this->scope instanceof WactClassCodeWriter) { - $this->scope->registerFunction($function); - return; - } - + public function registerFunction(WactFunctionCodeWriter $function) { $name = $function->getName(); if ($this->functionExists($name)) { require_once 'wact/compiler/codewriter/codewriter.xcpt.php'; throw new WactFunctionExistsException($name); } $this->functions[$name] = $function; - $this->scope = $function; + $this->setScope($function); return $name; } /** + * Registers a method. + * Checks that no function with this name has been registered yet. + * Calling this function results in an implicit scope switch. + * (sets the active scope to the newly registered function) + * @param WactMethodCodeWriter $function + * @throws WactFunctionExistsException + * @return string Function name. You need to apply the appropriate prefix ('$'/'$this->' etc.) manually if needed. + */ + public function registerMethod(WactMethodCodeWriter $method) { + return $this->scope->registerMethod($method); + } + + /** * Returns a function that has been registered previously. * Checks either current class scope or global scope. * @param string $name Name of the function to find - * @return WactFunctionCodeWriter or null + * @return WactFunctionCodeWriter|NULL */ public function findFunction($name) { - if ($this->scope instanceof WactClassCodeWriter) { - return $this->scope->findFunction($name); - } - if (array_key_exists($name, $this->functions)) { return $this->functions[$name]; } @@ -1104,20 +1189,36 @@ } /** + * Returns a method that has been registered previously. + * Checks either current class scope or global scope. + * @param string $name Name of the function to find + * @return WactMethodCodeWriter|NULL + */ + public function findMethod($name) { + return $this->scope->findMethod($name); + } + + /** * Checks if a function with the specified name is registered. * Checks either current class scope or global scope. * @param string $name The name to check * @return boolean */ public function functionExists($name) { - if ($this->scope instanceof WactClassCodeWriter) { - return $this->scope->findFunction($name); - } - return ($this->findFunction($name) != null); } /** + * Checks if a method with the specified name is registered. + * Checks either current class scope or global scope. + * @param string $name The name to check + * @return boolean + */ + public function methodExists($name) { + return $this->scope->findMethod($name); + } + + /** * Creates a new WactClassCodeWriter and registers it with this class. * Calling this function results in an implicit scope switch * (sets the active scope to the newly registered class). @@ -1149,7 +1250,7 @@ } $this->classes[$name] = $class; - $this->scope = $class; + $this->setScope($class); return $name; } Modified: trunk/wact/compiler/codewriter/codewriter.test.php =================================================================== --- trunk/wact/compiler/codewriter/codewriter.test.php 2007-10-21 18:44:52 UTC (rev 592) +++ trunk/wact/compiler/codewriter/codewriter.test.php 2007-10-21 20:29:11 UTC (rev 593) @@ -329,8 +329,8 @@ $class->registerInclude('test.php'); $class->registerInclude('test2.php'); - $funcName = $class->createFunction('init'); - $function = $class->findFunction('init'); + $funcName = $class->createMethod('init'); + $function = $class->findMethod('init'); $function->registerInclude('test3.php'); $class->registerInclude('test4.php'); @@ -348,18 +348,18 @@ $class = new WactClassCodeWriter('MyView'); $name = 'render'; - $function = new WactFunctionCodeWriter($name); - $funcName = $class->registerFunction($function); + $function = new WactMethodCodeWriter($name); + $funcName = $class->registerMethod($function); $this->assertEqual($name, $funcName); try { - $class->registerFunction($function); + $class->registerMethod($function); $this->fail('Expected exception not thrown'); } catch (WactFunctionExistsException $e) { $this->pass(); } - $function2 = $class->findFunction('render'); + $function2 = $class->findMethod('render'); $this->assertIdentical($function, $function2); $this->assertEqual($class->getCode(), 'class MyView {' . "\n\n" . 'function render() {' . "\n\n" . '}' . "\n\n" . '}' . "\n"); @@ -404,7 +404,7 @@ } $function = new WactFunctionCodeWriter('render'); - $class->registerFunction($function); + $class->registerMethod($function); $class->switchToPHP(); $class->switchToHTML(); @@ -420,7 +420,7 @@ $class = new WactClassCodeWriter('MyView'); $function = new WactFunctionCodeWriter('render'); - $class->registerFunction($function); + $class->registerMethod($function); $class->writePHP('if ($x = '); $class->writePHPLiteral('f\'o\'o'); @@ -436,12 +436,12 @@ $class = new WactClassCodeWriter('MyView'); $function = new WactFunctionCodeWriter('render'); - $class->registerFunction($function); + $class->registerMethod($function); // don't switch scope here (registerFunction() should do it automatically) $class->writePHP('echo 1;'); $function = new WactFunctionCodeWriter('render2'); - $class->registerFunction($function); + $class->registerMethod($function); $class->switchScope('render2'); $class->writePHP('echo 2;'); @@ -465,7 +465,7 @@ $class = new WactClassCodeWriter('MyView'); $function = new WactFunctionCodeWriter('render'); - $class->registerFunction($function); + $class->registerMethod($function); $class->switchScope(); @@ -494,11 +494,11 @@ function testSetScope() { $class = new WactClassCodeWriter('MyView'); - $class->createFunction('render'); - $renderFunc = $class->findFunction('render'); + $class->createMethod('render'); + $renderFunc = $class->findMethod('render'); - $class->createFunction('paint'); - $paintFunc = $class->findFunction('paint'); + $class->createMethod('paint'); + $paintFunc = $class->findMethod('paint'); $scope = $class->switchScope('render'); $this->assertIdentical($scope, $paintFunc); @@ -513,8 +513,8 @@ function testSwitchScopeReturns() { $class = new WactClassCodeWriter('MyView'); - $class->createFunction('render'); - $renderFunc = $class->findFunction('render'); + $class->createMethod('render'); + $renderFunc = $class->findMethod('render'); $scope = $class->switchScope(null); $this->assertIdentical($scope, $renderFunc); @@ -529,14 +529,14 @@ $name = 'init'; $params = '$p1'; - $funcName = $class->createFunction($name, $params); - $function = $class->findFunction($funcName); + $funcName = $class->createMethod($name, $params); + $function = $class->findMethod($funcName); $this->assertEqual($function->getName(), $name); $this->assertEqual($function->getParams(), $params); - $this->assertIdentical($function, $class->findFunction($name)); + $this->assertIdentical($function, $class->findMethod($name)); try { - $function = $class->createFunction($name, $params); + $function = $class->createMethod($name, $params); $this->fail('Expected exception not thrown'); } catch (WactFunctionExistsException $e) { $this->pass(); @@ -604,7 +604,7 @@ $this->pass(); } - $class->createFunction('init'); + $class->createMethod('init'); $var = $class->createTempVariable(); @@ -675,7 +675,7 @@ $code->registerClass($class); $function = new WactFunctionCodeWriter('render'); - $class->registerFunction($function); + $class->registerMethod($function); $code->writePHP('if ($x = '); $code->writePHPLiteral('f\'o\'o'); @@ -693,7 +693,7 @@ $class = new WactClassCodeWriter('MyView'); $classfunction = new WactFunctionCodeWriter('render'); - $class->registerFunction($classfunction); + $class->registerMethod($classfunction); $code = new WactCodeWriter(); $code->registerFunction($function); @@ -909,13 +909,6 @@ } try { - $code->registerProperty($name); - $this->fail('Expected exception not thrown'); - } catch (WactInvalidScopeException $e) { - $this->pass(); - } - - try { $code->findProperty($name); $this->fail('Expected exception not thrown'); } catch (WactInvalidScopeException $e) { @@ -973,16 +966,16 @@ $this->assertEqual($name, $className); $class = $code->findClass($className); - $code->createFunction('render'); - $this->assertTrue($class->functionExists('render')); + $code->createMethod('render'); + $this->assertTrue($class->methodExists('render')); - $function = new WactFunctionCodeWriter('init'); - $code->registerFunction($function); - $this->assertTrue($class->functionExists('init')); - $this->assertTrue($code->functionExists('init')); + $function = new WactMethodCodeWriter('init'); + $code->registerMethod($function); + $this->assertTrue($class->methodExists('init')); + $this->assertTrue($code->methodExists('init')); - $function2 = $code->findFunction('init'); - $function3 = $class->findFunction('init'); + $function2 = $code->findMethod('init'); + $function3 = $class->findMethod('init'); $this->assertIdentical($function, $function2); $this->assertIdentical($function2, $function3); Modified: trunk/wact/template/compiler/node/view.inc.php =================================================================== --- trunk/wact/template/compiler/node/view.inc.php 2007-10-21 18:44:52 UTC (rev 592) +++ trunk/wact/template/compiler/node/view.inc.php 2007-10-21 20:29:11 UTC (rev 593) @@ -50,8 +50,8 @@ if (!empty($this->runtimeIncludeFile)) { $code->registerInclude($this->runtimeIncludeFile); } - $code->createFunction('paint'); - $code->createFunction('__construct'); + $code->createMethod('paint'); + $code->createMethod('__construct'); parent::renderPaint($code); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |