Commit e33aa9fc by Carsten Brandt

simplified model and context creation

parent b17c8270
...@@ -22,6 +22,23 @@ class BaseDoc extends Object ...@@ -22,6 +22,23 @@ class BaseDoc extends Object
public $startLine; public $startLine;
public $endLine; public $endLine;
/**
* @param \phpDocumentor\Reflection\BaseReflector $reflector
* @param array $config
*/
public function __construct($reflector, $config = [])
{
// base properties
$this->name = ltrim($reflector->getName(), '\\');
$this->startLine = $reflector->getNode()->getAttribute('startLine');
$this->endLine = $reflector->getNode()->getAttribute('endLine');
// TODO docblock
parent::__construct($config);
}
public function loadSource($reflection) public function loadSource($reflection)
{ {
$this->sourcePath=str_replace('\\','/',str_replace(YII_PATH,'',$reflection->getFileName())); $this->sourcePath=str_replace('\\','/',str_replace(YII_PATH,'',$reflection->getFileName()));
......
...@@ -19,37 +19,23 @@ class ClassDoc extends BaseDoc ...@@ -19,37 +19,23 @@ class ClassDoc extends BaseDoc
public $interfaces = []; public $interfaces = [];
public $traits = []; public $traits = [];
// will be set by Context::updateReferences()
public $subclasses = [];
// TODO
public $properties = []; public $properties = [];
public $methods = []; public $methods = [];
public $events = []; public $events = [];
public $constants = []; public $constants = [];
// public $protectedPropertyCount=0;
// public $publicPropertyCount=0;
// public $protectedMethodCount=0;
// public $publicMethodCount=0;
//
// public $nativePropertyCount=0;
// public $nativeMethodCount=0;
// public $nativeEventCount=0;
public $sinceVersion;
public $subclasses = [];
/** /**
* @param \phpDocumentor\Reflection\ClassReflector $reflector * @param \phpDocumentor\Reflection\ClassReflector $reflector
* @param Context $context
* @param array $config * @param array $config
*/ */
public function __construct($reflector, $context = null, $config = []) public function __construct($reflector, $config = [])
{ {
// base properties parent::__construct($reflector, $config);
$this->name = ltrim($reflector->getName(), '\\');
$this->startLine = $reflector->getNode()->getAttribute('startLine');
$this->endLine = $reflector->getNode()->getAttribute('endLine');
$this->parentClass = ltrim($reflector->getParentClass(), '\\'); $this->parentClass = ltrim($reflector->getParentClass(), '\\');
if (empty($this->parentClass)) { if (empty($this->parentClass)) {
...@@ -68,13 +54,5 @@ class ClassDoc extends BaseDoc ...@@ -68,13 +54,5 @@ class ClassDoc extends BaseDoc
// TODO methods // TODO methods
// TODO properties // TODO properties
// TODO docblock
if ($context !== null) {
$context->addClass($this);
}
parent::__construct($config);
} }
} }
\ No newline at end of file
...@@ -8,15 +8,16 @@ ...@@ -8,15 +8,16 @@
namespace yii\apidoc\models; namespace yii\apidoc\models;
use phpDocumentor\Reflection\FileReflector;
use yii\base\Component; use yii\base\Component;
use yii\base\Exception; use yii\base\Exception;
class Context extends Component class Context extends Component
{ {
public $basePath; /**
* @var array list of php files that have been added to this context.
*/
public $files = []; public $files = [];
/** /**
* @var ClassDoc[] * @var ClassDoc[]
*/ */
...@@ -30,32 +31,66 @@ class Context extends Component ...@@ -30,32 +31,66 @@ class Context extends Component
*/ */
public $traits = []; public $traits = [];
public function addFile($fileName) public function addFile($fileName)
{ {
$file = new File($fileName, $this); if (isset($this->files[$fileName])) {
$this->files[$fileName] = $file; return;
}
$this->files[$fileName] = $fileName;
$reflection = new FileReflector($fileName, true);
$reflection->process();
foreach($reflection->getClasses() as $class) {
$class = new ClassDoc($class, $this);
$class->sourceFile = $fileName;
$this->addClass($class);
}
foreach($reflection->getInterfaces() as $interface) {
$interface = new InterfaceDoc($interface, $this);
$interface->sourceFile = $fileName;
$this->addInterface($interface);
}
foreach($reflection->getTraits() as $trait) {
$trait = new TraitDoc($trait, $this);
$trait->sourceFile = $fileName;
$this->addTrait($trait);
}
} }
/**
* @param ClassDoc $class
* @throws \yii\base\Exception when class is already part of this context
*/
public function addClass($class) public function addClass($class)
{ {
if (isset($this->classes[$class->name])) { if (isset($this->classes[$class->name])) {
throw new Exception('Duplicate class definition: ' . $class->name . ' in file ' . $class->fileName); throw new Exception('Duplicate class definition: ' . $class->name . ' in file ' . $class->sourceFile . '.');
} }
$this->classes[$class->name] = $class; $this->classes[$class->name] = $class;
} }
/**
* @param InterfaceDoc $interface
* @throws \yii\base\Exception when interface is already part of this context
*/
public function addInterface($interface) public function addInterface($interface)
{ {
if (isset($this->interfaces[$interface->name])) { if (isset($this->interfaces[$interface->name])) {
throw new Exception('Duplicate interface definition: ' . $interface->name . ' in file ' . $interface->fileName); throw new Exception('Duplicate interface definition: ' . $interface->name . ' in file ' . $interface->sourceFile);
} }
$this->interfaces[$interface->name] = $interface; $this->interfaces[$interface->name] = $interface;
} }
/**
* @param TraitDoc $trait
* @throws \yii\base\Exception when trait is already part of this context
*/
public function addTrait($trait) public function addTrait($trait)
{ {
if (isset($this->traits[$trait->name])) { if (isset($this->traits[$trait->name])) {
throw new Exception('Duplicate trait definition: ' . $trait->name . ' in file ' . $trait->fileName); throw new Exception('Duplicate trait definition: ' . $trait->name . ' in file ' . $trait->sourceFile);
} }
$this->traits[$trait->name] = $trait; $this->traits[$trait->name] = $trait;
} }
...@@ -74,7 +109,19 @@ class Context extends Component ...@@ -74,7 +109,19 @@ class Context extends Component
foreach($this->classes as $class) { foreach($this->classes as $class) {
$this->updateSubclassInferfacesTraits($class); $this->updateSubclassInferfacesTraits($class);
} }
// TODO update interface and trait usages // update implementedBy and usedBy for interfaces and traits
foreach($this->classes as $class) {
foreach($class->interfaces as $interface) {
if (isset($this->interfaces[$interface])) {
$this->interfaces[$interface]->implementedBy[] = $class->name;
}
}
foreach($class->traits as $trait) {
if (isset($this->traits[$trait])) {
$this->traits[$trait]->usedBy[] = $class->name;
}
}
}
} }
/** /**
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\apidoc\models;
use phpDocumentor\Reflection\FileReflector;
use yii\base\Object;
class File extends Object
{
public $name;
public $namespaces = [];
/**
* @var ClassDoc[]
*/
public $classes = [];
/**
* @var InterfaceDoc[]
*/
public $interfaces = [];
/**
* @var TraitDoc[]
*/
public $traits = [];
private $_reflection;
public function __construct($fileName, $context, $config = [])
{
$this->name = $fileName;
$this->_reflection = new FileReflector($fileName, true);
$this->_reflection->process();
foreach($this->_reflection->getClasses() as $class) {
$class = new ClassDoc($class, $context);
$class->sourceFile = $fileName;
$this->classes[] = $class;
}
foreach($this->_reflection->getInterfaces() as $interface) {
$this->interfaces[] = new InterfaceDoc($interface, $context);
}
foreach($this->_reflection->getTraits() as $trait) {
$this->traits[] = new TraitDoc($trait, $context);
}
parent::__construct($config);
}
}
\ No newline at end of file
...@@ -11,35 +11,24 @@ class InterfaceDoc extends BaseDoc ...@@ -11,35 +11,24 @@ class InterfaceDoc extends BaseDoc
{ {
public $parentInterfaces = []; public $parentInterfaces = [];
// will be set by Context::updateReferences()
public $implementedBy = []; public $implementedBy = [];
// TODO
public $methods = []; public $methods = [];
/** /**
* @param \phpDocumentor\Reflection\InterfaceReflector $reflector * @param \phpDocumentor\Reflection\InterfaceReflector $reflector
* @param Context $context
* @param array $config * @param array $config
*/ */
public function __construct($reflector, $context = null, $config = []) public function __construct($reflector, $config = [])
{ {
// base properties parent::__construct($reflector, $config);
$this->name = ltrim($reflector->getName(), '\\');
$this->startLine = $reflector->getNode()->getAttribute('startLine');
$this->endLine = $reflector->getNode()->getAttribute('endLine');
foreach($reflector->getParentInterfaces() as $interface) { foreach($reflector->getParentInterfaces() as $interface) {
$this->parentInterfaces[] = ltrim($interface, '\\'); $this->parentInterfaces[] = ltrim($interface, '\\');
} }
// TODO methods // TODO methods
// TODO docblock
if ($context !== null) {
$context->addInterface($this);
}
parent::__construct($config);
} }
} }
\ No newline at end of file
...@@ -10,25 +10,23 @@ namespace yii\apidoc\models; ...@@ -10,25 +10,23 @@ namespace yii\apidoc\models;
class TraitDoc extends BaseDoc class TraitDoc extends BaseDoc
{ {
// classes using the trait // classes using the trait
public $usedByClasses = []; // will be set by Context::updateReferences()
public $usedBy = [];
public $traits = []; public $traits = [];
// TODO
public $properties = []; public $properties = [];
public $methods = []; public $methods = [];
/** /**
* @param \phpDocumentor\Reflection\TraitReflector $reflector * @param \phpDocumentor\Reflection\TraitReflector $reflector
* @param Context $context
* @param array $config * @param array $config
*/ */
public function __construct($reflector, $context = null, $config = []) public function __construct($reflector, $config = [])
{ {
// base properties parent::__construct($reflector, $config);
$this->name = ltrim($reflector->getName(), '\\');
$this->startLine = $reflector->getNode()->getAttribute('startLine');
$this->endLine = $reflector->getNode()->getAttribute('endLine');
foreach($reflector->getTraits() as $trait) { foreach($reflector->getTraits() as $trait) {
$this->traits[] = ltrim($trait, '\\'); $this->traits[] = ltrim($trait, '\\');
...@@ -38,12 +36,5 @@ class TraitDoc extends BaseDoc ...@@ -38,12 +36,5 @@ class TraitDoc extends BaseDoc
// TODO properties // TODO properties
// TODO docblock
if ($context !== null) {
$context->addTrait($this);
}
parent::__construct($config);
} }
} }
\ No newline at end of file
...@@ -15,6 +15,12 @@ use yii\apidoc\models\TraitDoc; ...@@ -15,6 +15,12 @@ use yii\apidoc\models\TraitDoc;
} elseif ($item instanceof TraitDoc) { } elseif ($item instanceof TraitDoc) {
echo 'Trait '; echo 'Trait ';
} else { } else {
if ($item->isFinal) {
echo 'Final ';
}
if ($item->isAbstract) {
echo 'Abstract ';
}
echo 'Class '; echo 'Class ';
} }
echo $item->name; echo $item->name;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment