Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 4938b48d62894b37472fa19e37831585f8b09474 0 parents
@sasezaki authored
Showing with 96 additions and 0 deletions.
  1. +83 −0 exceptionclassgenerator.php
  2. +13 −0 run.php
83 exceptionclassgenerator.php
@@ -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;
+ }
+}
+
13 run.php
@@ -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]);
Please sign in to comment.
Something went wrong with that request. Please try again.