Compare commits

..
6 Commits
Author SHA1 Message Date
Barry vd. Heuvel b0b324b8cc Use reflection and reflectiondocblock
Load the original docblock and append the changes. This way, you can
just copy the new comments in whole (next step, replace in file..)
2013-05-30 11:54:45 +02:00
Barry vd. Heuvel dab594804b Map enum to string
Fixes #19
2013-05-30 10:15:56 +02:00
Barry vd. Heuvel 83d5fe048d Revert changes 2013-05-29 17:32:38 +02:00
Barry vd. Heuvel 77b1bc923e Set mapping for enum 2013-05-29 18:31:33 +03:00
Barry vd. Heuvel 9090aeb766 Catch getType
Hopefully fixes #19
2013-05-29 18:24:43 +03:00
Barry vd. Heuvel a5c16b0751 update readme 2013-05-29 10:28:23 +02:00
2 changed files with 60 additions and 15 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
## Laravel IDE Helper & Generator ## Laravel IDE Helper Generator
### Complete phpDocs, directly from the source ### Complete phpDocs, directly from the source
+59 -14
View File
@@ -13,6 +13,10 @@ use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\ClassLoader\ClassMapGenerator; use Symfony\Component\ClassLoader\ClassMapGenerator;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Context;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
/** /**
* A command to generate autocomplete information for your IDE * A command to generate autocomplete information for your IDE
* *
@@ -136,17 +140,23 @@ class ModelsCommand extends Command {
return $models; return $models;
} }
/**
* Load the properties from the database table.
*
* @param \Illuminate\Database\Eloquent\Model $model
*/
protected function getPropertiesFromTable($model){ protected function getPropertiesFromTable($model){
$table = $model->getTable(); $table = $model->getTable();
$schema = $model->getConnection()->getDoctrineSchemaManager($table); $schema = $model->getConnection()->getDoctrineSchemaManager($table);
$schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
$columns = $schema->listTableColumns($table); $columns = $schema->listTableColumns($table);
$properties = array();
if($columns){ if($columns){
foreach ($columns as $column) { foreach ($columns as $column) {
$name = $column->getName(); $name = $column->getName();
$type = $column->getType()->getName(); $type = $column->getType()->getName();
switch($type){ switch($type){
case 'string': case 'string':
case 'text': case 'text':
@@ -180,6 +190,9 @@ class ModelsCommand extends Command {
} }
} }
/**
* @param \Illuminate\Database\Eloquent\Model $model
*/
protected function getPropertiesFromMethods($model){ protected function getPropertiesFromMethods($model){
$methods = get_class_methods($model); $methods = get_class_methods($model);
if($methods){ if($methods){
@@ -242,6 +255,12 @@ class ModelsCommand extends Command {
} }
} }
/**
* @param string $name
* @param string|null $type
* @param bool|null $read
* @param bool|null $write
*/
protected function setProperty($name, $type = null, $read = null, $write = null){ protected function setProperty($name, $type = null, $read = null, $write = null){
if(!isset($this->properties[$name])){ if(!isset($this->properties[$name])){
$this->properties[$name] = array(); $this->properties[$name] = array();
@@ -270,18 +289,36 @@ class ModelsCommand extends Command {
} }
} }
/**
* @param string $class
* @return string
*/
protected function createPhpDocs($class){ protected function createPhpDocs($class){
if(strpos($class, '\\') !== false){ $reflection = new \ReflectionClass($class);
$parts = explode('\\', $class); $namespace = $reflection->getNamespaceName();
$class = array_pop($parts); $phpdoc = new DocBlock($reflection, new Context($namespace));
$namespace = implode($parts, '\\');
}else{ if(!$phpdoc->getText()){
$namespace = ''; $phpdoc->setText("Generated properties for $class");
} }
$output = "namespace $namespace{\n";
$output .= "\t/**\n\t *\n\t * Generated properties for $class\n\t *\n"; $properties = array();
$methods = array();
foreach($phpdoc->getTags() as $tag){
$name = $tag->getName();
if($name == "property" || $name == "property-read" || $name == "property-write"){
$properties[] =$tag->getVariableName();
}elseif($name == "method"){
$methods[] = $tag->getMethodName();
}
}
foreach($this->properties as $name => $property){ foreach($this->properties as $name => $property){
$name = "\$$name";
if(in_array($name, $properties)){
continue;
}
if($property['read'] && $property['write']){ if($property['read'] && $property['write']){
$attr = 'property'; $attr = 'property';
}elseif($property['write']){ }elseif($property['write']){
@@ -289,16 +326,24 @@ class ModelsCommand extends Command {
}else{ }else{
$attr = 'property-read'; $attr = 'property-read';
} }
$type = $property['type']; $tag = Tag::createInstance("@{$attr} {$property['type']} {$name}", $phpdoc);
$output .= "\t * @$attr $type \$$name \n"; $phpdoc->appendTag($tag);
} }
foreach($this->methods as $name => $method){ foreach($this->methods as $name => $method){
$type = $method['type']; if(in_array($name, $methods)){
$output .= "\t * @method $type $name() \n"; continue;
}
$name = "$name()";
$tag = Tag::createInstance("@method {$method['type']} {$name}", $phpdoc);
$phpdoc->appendTag($tag);
} }
$output .= "\t *\n\t */\n\tclass $class {}\n}\n\n"; $serializer = new DocBlockSerializer(1, "\t");
$serializer->getDocComment($phpdoc);
$docComment = $serializer->getDocComment($phpdoc);
$output = "namespace {$namespace}{\n{$docComment}\n\tclass {$class} {}\n}\n\n";
return $output; return $output;
} }