Skip to content

Commit

Permalink
Merge pull request laruence#36 from fruit/yaf-classes-skeleton
Browse files Browse the repository at this point in the history
Adds namespaces support for Yaf`s skeleton class generator
  • Loading branch information
laruence committed Apr 21, 2013
2 parents 4a95643 + edab239 commit 99472cd
Showing 1 changed file with 154 additions and 118 deletions.
272 changes: 154 additions & 118 deletions tools/yaf.php
Original file line number Diff line number Diff line change
@@ -1,133 +1,169 @@
<?php
/**
* Yaf Classes signature generator
*
*
* @author Laruence
* @date 2012-07-21 13:46
* @version $Id$
*/
* @version $Id$
*/

$useNamespace = (bool) ini_get("yaf.use_namespace");
$yafClassPrefix = sprintf("Yaf%s", $useNamespace ? "\\" : "_");
$classes = get_declared_classes();
foreach ($classes as $key => $value) {
if (strncasecmp($value, "Yaf_", 3)) {
if (strncasecmp($value, $yafClassPrefix, 4)) {
unset($classes[$key]);
}
}

echo "<?php\n";

foreach ($classes as $class_name) {
$class = new ReflectionClass($class_name);
$indent = "";

if ($class->isFinal()) {
echo "final ";
}

if ($class->isAbstract()) {
echo "abstract ";
}

if ($class->isInterface()) {
echo "Interface ", $class_name;
} else {
echo "class ", $class_name;
}

/* parent */
$parent = $class->getParentClass();
if ($parent) {
echo " extends ", $parent->getName();
}

/* interface */
$interfaces = $class->getInterfaceNames();
if (count($interfaces)) {
echo " implements ", join(", ", $interfaces);
}
echo " { \n";

$indent .= "\t";
/* constants */
echo $indent, "/* constants */\n";
$constatnts = $class->getConstants();
foreach ($constatnts as $k => $v) {
echo $indent, "const ", $k , " = ", $v , ";\n";
}
echo "\n";

/* properties */
echo $indent, "/* properties */\n";
$properties = $class->getProperties();
$values = $class->getDefaultProperties();
foreach ($properties as $p) {
echo $indent;
if ($p->isStatic()) {
echo "static ";
}

if ($p->isPublic()) {
echo "public ";
} else if ($p->isProtected()) {
echo "protected ";
} else {
echo "private ";
}

echo '$', $p->getName(), " = ";

if (isset($values[$p->getName()])) {
echo '"', $values[$p->getName()], '"';
} else {
echo "NULL";
}
echo ";\n";
}
echo "\n";

/* methods */
echo $indent, "/* methods */\n";
$methods = $class->getMethods();
foreach ($methods as $m) {
echo $indent;
echo implode(' ', Reflection::getModifierNames($m->getModifiers()));
echo " function ", $m->getName(), "(";
$parameters = $m->getParameters();
$number = count($parameters);
$index = 0;
foreach ($parameters as $a) {
if (($type = $a->getClass())) {
echo $type->getName(), " ";
} else if ($a->isArray()) {
echo "array ";
}

if ($a->isPassedByReference()) {
echo "&";
}

$name = $a->getName();
if ($name == "...") {
echo '$_ = "..."';
} else {
echo "$", $name;
}

if ($a->isOptional()) {
if ($a->isDefaultValueAvailable()) {
echo " = ", $a->getDefaultValue();
} else {
echo " = NULL";
}
}

if (++$index < $number) {
echo ", ";
}
}

echo ") {\n";
echo $indent, "}\n";
}
echo "}\n\n";
}
$class = new ReflectionClass($class_name);
$indent = "";

if ($useNamespace && false !== ($backslash = strrpos($class_name, "\\"))) {
$namespaceName = substr($class_name, 0, $backslash);
$class_name = substr($class_name, $backslash + 1);
echo "namespace ", $namespaceName, " {\n";

$indent = "\t";
}

$classAttributes = "";

if ($class->isInterface()) {
$classAttributes .= "interface ";
} else {
if ($class->isFinal()) {
$classAttributes .= "final ";
}

if ($class->isAbstract()) {
$classAttributes .= "abstract ";
}

$classAttributes .= "class ";
}

echo $indent, $classAttributes, $class_name;

/* parent */
$parent = $class->getParentClass();
if ($parent) {
echo " extends ", $parent->getName();
}

/* interface */
$interfaces = $class->getInterfaceNames();
if (count($interfaces)) {
echo " implements ", join(", ", $interfaces);
}
echo " {\n";

$indent .= "\t";
/* constants */
$constants = $class->getConstants();
if (0 < count($constants)) {
echo $indent, "/* constants */\n";

foreach ($constants as $k => $v) {
echo $indent, "const ", $k , " = ", $v , ";\n";
}
echo "\n";
}

/* properties */
$properties = $class->getProperties();
if (0 < count($properties)) {
echo $indent, "/* properties */\n";
$values = $class->getDefaultProperties();
foreach ($properties as $p) {
echo $indent;

if ($p->isStatic()) {
echo "static ";
}

if ($p->isPublic()) {
echo "public ";
} else if ($p->isProtected()) {
echo "protected ";
} else {
echo "private ";
}

echo '$', $p->getName(), " = ";

if (isset($values[$p->getName()])) {
echo '"', $values[$p->getName()], '"';
} else {
echo "NULL";
}
echo ";\n";
}
echo "\n";
}

/* methods */
$methods = $class->getMethods();
if (0 < count($methods)) {
echo $indent, "/* methods */\n";

foreach ($methods as $m) {
echo $indent;
echo implode(' ', Reflection::getModifierNames($m->getModifiers()));
echo " function ", $m->getName(), "(";

if ($m->isAbstract()) {
// abstract methods are without a body "{ ... }"
echo ");\n";
continue;
}

$parameters = $m->getParameters();
$number = count($parameters);
$index = 0;
foreach ($parameters as $a) {
if (($type = $a->getClass())) {
echo $type->getName(), " ";
} else if ($a->isArray()) {
echo "array ";
}

if ($a->isPassedByReference()) {
echo "&";
}

$name = $a->getName();
if ($name == "...") {
echo '$_ = "..."';
} else {
echo "$", $name;
}

if ($a->isOptional()) {
if ($a->isDefaultValueAvailable()) {
echo " = ", $a->getDefaultValue();
} else {
echo " = NULL";
}
}

if (++$index < $number) {
echo ", ";
}
}

echo ") {\n";
echo $indent, "}\n";
}
}

$indent = substr($indent, 0, -1);
echo $indent, "}\n";

if ($useNamespace && false !== $backslash) {
echo "}\n\n";
}
}

0 comments on commit 99472cd

Please sign in to comment.