From e33aa9fc0dbc47c67698226ecaca52e79cf4525f Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 5 Jan 2014 05:04:54 +0100 Subject: [PATCH] simplified model and context creation --- extensions/yii/apidoc/models/BaseDoc.php | 17 +++++++ extensions/yii/apidoc/models/ClassDoc.php | 32 +++---------- extensions/yii/apidoc/models/Context.php | 65 +++++++++++++++++++++++---- extensions/yii/apidoc/models/File.php | 53 ---------------------- extensions/yii/apidoc/models/InterfaceDoc.php | 19 ++------ extensions/yii/apidoc/models/TraitDoc.php | 19 +++----- extensions/yii/apidoc/views/class.php | 6 +++ 7 files changed, 93 insertions(+), 118 deletions(-) delete mode 100644 extensions/yii/apidoc/models/File.php diff --git a/extensions/yii/apidoc/models/BaseDoc.php b/extensions/yii/apidoc/models/BaseDoc.php index 888d1ac..fa8c709 100644 --- a/extensions/yii/apidoc/models/BaseDoc.php +++ b/extensions/yii/apidoc/models/BaseDoc.php @@ -22,6 +22,23 @@ class BaseDoc extends Object public $startLine; 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) { $this->sourcePath=str_replace('\\','/',str_replace(YII_PATH,'',$reflection->getFileName())); diff --git a/extensions/yii/apidoc/models/ClassDoc.php b/extensions/yii/apidoc/models/ClassDoc.php index 3fe7f3d..3d33568 100644 --- a/extensions/yii/apidoc/models/ClassDoc.php +++ b/extensions/yii/apidoc/models/ClassDoc.php @@ -19,37 +19,23 @@ class ClassDoc extends BaseDoc public $interfaces = []; public $traits = []; + // will be set by Context::updateReferences() + public $subclasses = []; + // TODO public $properties = []; public $methods = []; public $events = []; 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 Context $context * @param array $config */ - public function __construct($reflector, $context = null, $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'); + parent::__construct($reflector, $config); $this->parentClass = ltrim($reflector->getParentClass(), '\\'); if (empty($this->parentClass)) { @@ -68,13 +54,5 @@ class ClassDoc extends BaseDoc // TODO methods // TODO properties - - // TODO docblock - - if ($context !== null) { - $context->addClass($this); - } - - parent::__construct($config); } } \ No newline at end of file diff --git a/extensions/yii/apidoc/models/Context.php b/extensions/yii/apidoc/models/Context.php index 11ece3e..8c3d658 100644 --- a/extensions/yii/apidoc/models/Context.php +++ b/extensions/yii/apidoc/models/Context.php @@ -8,15 +8,16 @@ namespace yii\apidoc\models; +use phpDocumentor\Reflection\FileReflector; use yii\base\Component; use yii\base\Exception; class Context extends Component { - public $basePath; - + /** + * @var array list of php files that have been added to this context. + */ public $files = []; - /** * @var ClassDoc[] */ @@ -30,32 +31,66 @@ class Context extends Component */ public $traits = []; + public function addFile($fileName) { - $file = new File($fileName, $this); - $this->files[$fileName] = $file; + if (isset($this->files[$fileName])) { + 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) { 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; } + /** + * @param InterfaceDoc $interface + * @throws \yii\base\Exception when interface is already part of this context + */ public function addInterface($interface) { 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; } + /** + * @param TraitDoc $trait + * @throws \yii\base\Exception when trait is already part of this context + */ public function addTrait($trait) { 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; } @@ -74,7 +109,19 @@ class Context extends Component foreach($this->classes as $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; + } + } + } } /** diff --git a/extensions/yii/apidoc/models/File.php b/extensions/yii/apidoc/models/File.php deleted file mode 100644 index 61597c2..0000000 --- a/extensions/yii/apidoc/models/File.php +++ /dev/null @@ -1,53 +0,0 @@ -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 diff --git a/extensions/yii/apidoc/models/InterfaceDoc.php b/extensions/yii/apidoc/models/InterfaceDoc.php index 1275898..32f9113 100644 --- a/extensions/yii/apidoc/models/InterfaceDoc.php +++ b/extensions/yii/apidoc/models/InterfaceDoc.php @@ -11,35 +11,24 @@ class InterfaceDoc extends BaseDoc { public $parentInterfaces = []; + // will be set by Context::updateReferences() public $implementedBy = []; + // TODO public $methods = []; /** * @param \phpDocumentor\Reflection\InterfaceReflector $reflector - * @param Context $context * @param array $config */ - public function __construct($reflector, $context = null, $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'); + parent::__construct($reflector, $config); foreach($reflector->getParentInterfaces() as $interface) { $this->parentInterfaces[] = ltrim($interface, '\\'); } // TODO methods - - // TODO docblock - - if ($context !== null) { - $context->addInterface($this); - } - - parent::__construct($config); } - } \ No newline at end of file diff --git a/extensions/yii/apidoc/models/TraitDoc.php b/extensions/yii/apidoc/models/TraitDoc.php index 20de618..eef6b4e 100644 --- a/extensions/yii/apidoc/models/TraitDoc.php +++ b/extensions/yii/apidoc/models/TraitDoc.php @@ -10,25 +10,23 @@ namespace yii\apidoc\models; class TraitDoc extends BaseDoc { // classes using the trait - public $usedByClasses = []; + // will be set by Context::updateReferences() + public $usedBy = []; public $traits = []; + // TODO public $properties = []; public $methods = []; /** * @param \phpDocumentor\Reflection\TraitReflector $reflector - * @param Context $context * @param array $config */ - public function __construct($reflector, $context = null, $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'); + parent::__construct($reflector, $config); foreach($reflector->getTraits() as $trait) { $this->traits[] = ltrim($trait, '\\'); @@ -38,12 +36,5 @@ class TraitDoc extends BaseDoc // TODO properties - // TODO docblock - - if ($context !== null) { - $context->addTrait($this); - } - - parent::__construct($config); } } \ No newline at end of file diff --git a/extensions/yii/apidoc/views/class.php b/extensions/yii/apidoc/views/class.php index ffe6063..5c0ddaa 100644 --- a/extensions/yii/apidoc/views/class.php +++ b/extensions/yii/apidoc/views/class.php @@ -15,6 +15,12 @@ use yii\apidoc\models\TraitDoc; } elseif ($item instanceof TraitDoc) { echo 'Trait '; } else { + if ($item->isFinal) { + echo 'Final '; + } + if ($item->isAbstract) { + echo 'Abstract '; + } echo 'Class '; } echo $item->name;