Author: Frederik Holljen Date: 2006-01-13 10:45:56 +0100 (Fri, 13 Jan 2006) New Revision: 1814
Log: - new exception style (THE IMPENDING DOOM) Added: packages/PhpGenerator/trunk/src/exceptions/flow_exception.php Modified: packages/PhpGenerator/trunk/src/exceptions/php_generator_exception.php packages/PhpGenerator/trunk/src/php_generator.php packages/PhpGenerator/trunk/src/php_generator_autoload.php packages/PhpGenerator/trunk/tests/php_generator_test.php Added: packages/PhpGenerator/trunk/src/exceptions/flow_exception.php =================================================================== --- packages/PhpGenerator/trunk/src/exceptions/flow_exception.php 2006-01-13 09:30:30 UTC (rev 1813) +++ packages/PhpGenerator/trunk/src/exceptions/flow_exception.php 2006-01-13 09:45:56 UTC (rev 1814) @@ -0,0 +1,30 @@ +<?php +/** + * File containing the ezcPhpGeneratorFlowException class + * + * @package PhpGenerator + * @version //autogen// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Flow exceptions are thrown when control structures like if and while are closed out of order. + * + * @package PhpGenerator + * @version //autogen// + */ +class ezcPhpGeneratorFlowException extends ezcPhpGeneratorException +{ + /** + * Constructs a new flow exception. + * + * $expectedFlow is the name of the control structure you expected the end of + * and $calledFlow is the actual structure received. + */ + function __construct( $expectedFlow, $calledFlow ) + { + parent::__construct( "Expected end of {$expectedFlow} but got end of {$calledFlow}" ); + } +} + +?> Property changes on: packages/PhpGenerator/trunk/src/exceptions/flow_exception.php ___________________________________________________________________ Name: svn:eol-style + native Modified: packages/PhpGenerator/trunk/src/exceptions/php_generator_exception.php =================================================================== --- packages/PhpGenerator/trunk/src/exceptions/php_generator_exception.php 2006-01-13 09:30:30 UTC (rev 1813) +++ packages/PhpGenerator/trunk/src/exceptions/php_generator_exception.php 2006-01-13 09:45:56 UTC (rev 1814) @@ -13,59 +13,19 @@ * @package PhpGenerator * @version //autogen// */ -class ezcPhpGeneratorException extends Exception +class ezcPhpGeneratorException extends ezcBaseException { /** - * An invalid directory was specified. - */ - const INVALID_DIRECTORY = 1; - /** - * It was not not possible to open the file. - */ - const FILE_OPEN_FAILED = 2; - /** - * It was not possible to rename the temporary file. - */ - const FILE_RENAME_FAILED = 3; - /** - * It was not possible to write to the file. - */ - const FILE_WRITE_FAILED = 4; - - /** - * A control structure was ended out of order. + * Constructs a new ezcPhpGeneratorException with error message $message. * - * This will occur e.g if you start an if and then try to close a while. - * Example: - * <code> - * $php->appendWhile( '$myVar > 0' ); - * $php->appendEndIf(); // throws exception. - * </code> - */ - const FLOW_ERROR = 1000; - - /** - * Tried to save the generated PHP file with open FLOW structures. - * <code> - * $php->appendWhile( '$myVar > 0' ); - * $php->appendIf( '$myVar == 1' ); - * $php->appendEndIf(); - * $php->finish(); // throws exception. - * </code> - */ - const NESTING_ERROR = 1001; - - /** - * Constructs a new ezcPhpGeneratorException with error message $message and error code $code. - * * @param string $message * @param int $code * @return void */ - public function __construct( $message, $code ) + public function __construct( $message ) { - parent::__construct( $message, $code ); + parent::__construct( $message, 0 ); } } ?> Modified: packages/PhpGenerator/trunk/src/php_generator.php =================================================================== --- packages/PhpGenerator/trunk/src/php_generator.php 2006-01-13 09:30:30 UTC (rev 1813) +++ packages/PhpGenerator/trunk/src/php_generator.php 2006-01-13 09:45:56 UTC (rev 1814) @@ -177,7 +177,8 @@ * want to run the generated code using eval() later. $niceIndentation controls if the PHP output * should be indented correctly. This option is useful if you want to debug the generated code. * - * @throws PhpGeneratorException If the PhpGenerator can't open a temporary file for writing. + * @throws ezcBaseFileNotFounException if $filename does not contain a valid path. + * @throws ezcBaseFilePermissionException if the path specified by $filename is not writeable. * @param string $filename * @param bool $includeStartEndTags * @param bool $niceIndentation @@ -197,6 +198,15 @@ // setup file write resource $dir = dirname( $filename ); + if( !file_exists( $dir ) ) + { + throw new ezcBaseFileNotFoundException( $dir ); + } + else if( !is_writable( $dir ) ) + { + throw new ezcBaseFilePermissionException( $dir, ezcBaseFileException::WRITE ); + } + $file = basename( $filename ); // generate a temporary name @@ -210,8 +220,8 @@ $this->tmpFilename = null; $this->fileResource = null; $this->resultFilename = null; - throw new ezcPhpGeneratorException( 'ezcPhpGenerator could not open the file $fileName for writing.', - ezcPhpGeneratorException::FILE_OPEN_FAILED ); + throw new ezcBaseFilePermissionException( $file, ezcBaseFileException::WRITE, + "Failed to open temporary file even though the directory was writable." ); } if ( $this->includeStartEndTags ) { @@ -314,10 +324,10 @@ */ public function finish() { - if ( count( $this->flowStack ) != 0 ) + $count = count( $this->flowStack ); + if ( $count != 0 ) { - throw new ezcPhpGeneratorException( 'finish() called while still inside a flow control structure.', - ezcPhpGeneratorException::NESTING_ERROR ); + throw new ezcPhpGeneratorFlowException( $this->flowStack[$count-1], 'finish' ); } if ( $this->fileResource ) @@ -720,7 +730,7 @@ * } * </code> * - * @throws PhpGeneratorException if it was not possible to write the custom codeto the output file. + * @throws PhpGeneratorException if it was not possible to write the custom code to the output file. * @param string $code * @return void */ @@ -775,9 +785,9 @@ * * @see $ezcPhpGenerator::appendElse() * @see $ezcPhpGenerator::appendEndIf() + * @throws PhpGeneratorException if it was not possible to write the if statement to the output file. * @param string $condition * @return void - * @throws PhpGeneratorException if it was not possible to write the if statement to the output file. */ public function appendIf( $condition ) { @@ -854,8 +864,8 @@ else { $this->abort(); - $message = $pop ? "Expected end of <{$pop}>, but <{$type}> was requested" : 'Currently not in a flow control structure'; - throw new ezcPhpGeneratorException( $message, ezcPhpGeneratorException::FLOW_ERROR ); + $current = $pop ? $pop : 'no control structure'; + throw new ezcPhpGeneratorFlowException( $current, 'else' ); } } @@ -1003,8 +1013,7 @@ else { $this->abort(); - $message = $pop ? "Expected end of <{$pop}>, but <{$type}> was requested" : 'Currently not in a flow control structure'; - throw new ezcPhpGeneratorException( $message, ezcPhpGeneratorException::FLOW_ERROR ); + throw new ezcPhpGeneratorFlowException( $pop, 'do' ); } } @@ -1027,8 +1036,8 @@ else { $this->abort(); - $message = $pop ? "Expected end of <{$pop}>, but <{$type}> was requested" : 'Currently not in a flow control structure'; - throw new ezcPhpGeneratorException( $message, ezcPhpGeneratorException::FLOW_ERROR ); + $current = $pop ? $pop : 'no control structure'; + throw new ezcPhpGeneratorFlowException( $current, $type ); } } /** @@ -1041,15 +1050,14 @@ { if ( !$this->fileResource ) { - throw new ezcPhpGeneratorException( 'ezcPhpGenerator could not write to the temporary file. ' . - 'It has already been closed', - ezcPhpGeneratorException::FILE_WRITE_FAILED ); + throw new ezcBaseFileIoException( $this->tmpFilename, ezcBaseFileException::WRITE, + 'ezcPhpGenerator could not write to the temporary file. It has already been closed.' ); } if ( fwrite( $this->fileResource, $data ) === false ) { - throw new ezcPhpGeneratorException( 'ezcPhpGenerator could not write to the temporary file: ' . - $this->tmpFilename, ezcPhpGeneratorException::FILE_WRITE_FAILED ); + throw new ezcBaseFileIoException( $this->tmpFilename, ezcBaseFileException::WRITE, + 'ezcPhpGenerator could not write to the temporary file.' ); } } Modified: packages/PhpGenerator/trunk/src/php_generator_autoload.php =================================================================== --- packages/PhpGenerator/trunk/src/php_generator_autoload.php 2006-01-13 09:30:30 UTC (rev 1813) +++ packages/PhpGenerator/trunk/src/php_generator_autoload.php 2006-01-13 09:45:56 UTC (rev 1814) @@ -11,6 +11,7 @@ 'ezcPhpGeneratorReturnData' => 'PhpGenerator/structs/php_generator_return_data.php', 'ezcPhpGeneratorParameter' => 'PhpGenerator/structs/php_generator_parameter.php', 'ezcPhpGenerator' => 'PhpGenerator/php_generator.php', - 'ezcPhpGeneratorException' => 'PhpGenerator/exceptions/php_generator_exception.php' + 'ezcPhpGeneratorException' => 'PhpGenerator/exceptions/php_generator_exception.php', + 'ezcPhpGeneratorFlowException' => 'PhpGenerator/exceptions/flow_exception.php' ); ?> Modified: packages/PhpGenerator/trunk/tests/php_generator_test.php =================================================================== --- packages/PhpGenerator/trunk/tests/php_generator_test.php 2006-01-13 09:30:30 UTC (rev 1813) +++ packages/PhpGenerator/trunk/tests/php_generator_test.php 2006-01-13 09:45:56 UTC (rev 1814) @@ -26,23 +26,6 @@ } /** - * Tests writing to a dir that does not exist or without - * write permissions. - */ - public function testWriteToFaultyDir() - { - try - { - @$generator = new ezcPhpGenerator( "/no/such/path/or_file.php" ); - } - catch ( ezcPhpGeneratorException $e ) - { - return; - } - $this->fail( "Writing to a dir that does not exist did not fail." ); - } - - /** * Tests appendVariable with a normal assignment: = */ public function testAppendAssignment() @@ -153,10 +136,11 @@ { $generator->appendEndForeach(); } - catch ( ezcPhpGeneratorException $e ) + catch ( ezcPhpGeneratorFlowException $e ) { - $this->assertEquals( ezcPhpGeneratorException::FLOW_ERROR, $e->getCode() ); + return; } + $this->fail( "Expected exception" ); } /** @@ -509,7 +493,7 @@ { $generator->appendIf( 'true' ); } - catch ( ezcPhpGeneratorException $e ) + catch ( ezcBaseFileIoException $e ) { // eat, this is expected. return; @@ -532,7 +516,7 @@ { // eat, this is expected. return; - } + } $this->fail( "Finished file with improper nesting without getting an exception." ); } @@ -568,6 +552,23 @@ return $count; } + /** + * Tests writing to a dir that does not exist or without + * write permissions. + */ + public function testWriteToFaultyDir() + { + try + { + @$generator = new ezcPhpGenerator( "/no/such/path/or_file.php" ); + } + catch ( ezcBaseFileNotFoundException $e ) + { + return; + } + $this->fail( "Writing to a dir that does not exist did not fail." ); + } + public static function suite() { return new ezcTestSuite( "ezcPhpGeneratorTest" ); -- svn-components mailing list [email protected] http://lists.ez.no/mailman/listinfo/svn-components
