Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4938b48
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
use Zend\CodeGenerator\Php\PhpClass, | ||
Zend\CodeGenerator\Php\PhpFile, | ||
Zend\Reflection\ReflectionFile; | ||
|
||
|
||
class ExceptionClassGenerator | ||
{ | ||
private $docblock; | ||
|
||
public function setDocblock($docblock) | ||
{ | ||
$this->docblock = $docblock; | ||
} | ||
|
||
/** | ||
* | ||
* | ||
*/ | ||
public static function generateAll($baseInterfaceFile) | ||
{ | ||
foreach(static::getExceptionClasses() as $className) { | ||
$gen = new static; | ||
$gen->generateFile($className, $baseInterfaceFile); | ||
} | ||
|
||
} | ||
|
||
public function generateFile($className, $baseInterfaceFile) | ||
{ | ||
include_once $baseInterfaceFile; | ||
$refFile = new ReflectionFile($baseInterfaceFile); | ||
|
||
$interfaceName = $refFile->getClass()->getName(); | ||
|
||
$class = $this->generateClass($className, "$interfaceName", $interfaceName); | ||
$dir = substr($refFile->getFileName(), 0, strlen($refFile->getFileName()) - 4); | ||
$file = $dir . DIRECTORY_SEPARATOR . $className . '.php'; | ||
|
||
if (file_exists($file)) { | ||
return false; | ||
} | ||
|
||
if (!is_dir($dir)) { | ||
mkdir($dir); | ||
} | ||
|
||
$phpFile = new PhpFile; | ||
$phpFile->setClass($class); | ||
$phpFile->setFileName($file); | ||
|
||
$phpFile->write(); | ||
} | ||
|
||
public function generateClass($className, $namespaceName, $interfaceName) | ||
{ | ||
$codeClass = new PhpClass; | ||
if ($this->docblock) { | ||
$codeClass->setDocblock($this->docblock); | ||
} | ||
$codeClass->setName($className); | ||
$codeClass->setNamespaceName($namespaceName); | ||
$codeClass->setExtendedClass("\\$className"); | ||
$codeClass->setImplementedInterfaces(array("\\$interfaceName")); | ||
|
||
return $codeClass; | ||
} | ||
|
||
public static function getExceptionClasses() | ||
{ | ||
$exceptionClasses = array(); | ||
foreach (spl_classes() as $class) { | ||
if (!in_array('Exception', class_parents($class))) { | ||
continue; | ||
} | ||
$exceptionClasses[] = $class; | ||
} | ||
|
||
return $exceptionClasses; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
$zf2 = $_SERVER['HOME']. '/dev/zendframework_zf2/library'; | ||
require_once $zf2. '/Zend/Loader/StandardAutoloader.php'; | ||
use Zend\Loader\StandardAutoloader; | ||
|
||
$autoloader = new StandardAutoloader; | ||
$autoloader->register(); | ||
require 'exceptionclassgenerator.php'; | ||
|
||
ExceptionClassGenerator::generateAll($argv[1]); | ||
|
||
//$gen = new ExceptionClassGenerator; | ||
//$ret = $gen->generateFile('InvalidArgumentException', $argv[1]); |