Skip to content

Commit

Permalink
Merge pull request #38 from Firesphere/develop
Browse files Browse the repository at this point in the history
Fixed issue #37
  • Loading branch information
Firesphere committed Mar 30, 2016
2 parents 129b0be + 40f5300 commit a028c84
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 46 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This module generates @property, @method and @mixin tags for DataObjects and DataExtensions, so ide's like PHPStorm recognize the database and relations that are set in the $db, $has_one, $has_many and $many_many arrays.

All DataExtensions will be added to the docblock with the @mixin tag.
All DataExtensions will be added to the docblock with the @mixin tag.

The docblocks can be generated/updated with each dev/build and with a DataObjectAnnotatorTask per module or classname.

Expand Down Expand Up @@ -63,7 +63,8 @@ You can add extra module folders with the following config setting :
Config::inst()->update('DataObjectAnnotator', 'enabled_modules', array('mysite', 'otherfolderinsiteroot'));
```
or
```yml
```
yml
---
Only:
environment: 'dev'
Expand Down
27 changes: 23 additions & 4 deletions code/Annotatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
/**
* Class Annotatable
*
* Annotate the provided DataObjects for autocompletion purposes.
* Annotate extension for the provided DataObjects for autocompletion purposes.
* Start annotation, if skipannotation is not set and the annotator is enabled.
*
* @property DataObject|Annotatable owner
* @package IDEAnnotator
*
* @property DataObject|Annotatable $owner
*/
class Annotatable extends DataExtension
{
Expand Down Expand Up @@ -43,22 +45,39 @@ public function requireDefaultRecords()
return false;
}

$this->generateClassAnnotations();
$this->generateExtensionAnnotations();

return null;
}

/**
* Generate class own annotations
*/
private function generateClassAnnotations()
{
/* Annotate the current Class, if annotatable */
if ($this->permissionChecker->classNameIsAllowed($this->owner->ClassName)) {
$this->annotator->annotateDataObject($this->owner->ClassName);
DB::alteration_message($this->owner->ClassName . ' Annotated', 'created');
}
}

/**
* Generate class Extension annotations
*/
private function generateExtensionAnnotations()
{
/** @var array $extensions */
$extensions = Config::inst()->get($this->owner->ClassName, 'extensions', Config::UNINHERITED);
/* Annotate the extensions for this Class, if annotatable */
if ($extensions) {
foreach ($extensions as $extension) {
if ($this->permissionChecker->classNameIsAllowed($extension)) {
$this->annotator->annotateDataObject($extension);
DB::alteration_message($extension . ' Annotated', 'created');
}
}
}

return null;
}
}
39 changes: 23 additions & 16 deletions code/DataObjectAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* The generation is disabled by default.
* It is advisable to only enable it in your local dev environment,
* so the files won't change on a production server when you run dev/build
*
* @package IDEAnnotator
*/
class DataObjectAnnotator extends Object
{
Expand All @@ -28,12 +30,14 @@ class DataObjectAnnotator extends Object
/**
* @config
* Enable generation from @see Annotatable and @see DataObjectAnnotatorTask
* @var bool
*/
private static $enabled = false;

/**
* @config
* Enable modules that are allowed to have generated docblocks for DataObjects and DataExtensions
* @var array
*/
private static $enabled_modules = array('mysite');

Expand All @@ -52,7 +56,10 @@ class DataObjectAnnotator extends Object
'Extensions',
);

protected $annotatePermissionChecker;
/**
* @var AnnotatePermissionChecker
*/
private $permissionChecker;

/**
* All classes that subclass DataObject
Expand Down Expand Up @@ -85,7 +92,7 @@ public function __construct()
$this->classes = ClassInfo::subclassesFor('DataObject');
$this->extensionClasses = ClassInfo::subclassesFor('Object');
$this->dataExtensions = ClassInfo::subclassesFor('DataExtension');
$this->annotatePermissionChecker = Injector::inst()->get('AnnotatePermissionChecker');
$this->permissionChecker = Injector::inst()->get('AnnotatePermissionChecker');
}

/**
Expand All @@ -99,7 +106,7 @@ public function __construct()
*/
public function annotateModule($moduleName, $undo = false)
{
if (!$this->annotatePermissionChecker->moduleIsAllowed($moduleName)) {
if (!$this->permissionChecker->moduleIsAllowed($moduleName)) {
return false;
}

Expand All @@ -126,11 +133,11 @@ public function annotateModule($moduleName, $undo = false)
*/
public function annotateDataObject($className, $undo = false)
{
if (!$this->annotatePermissionChecker->classNameIsAllowed($className)) {
if (!$this->permissionChecker->classNameIsAllowed($className)) {
return false;
}

$filePath = $this->annotatePermissionChecker->getClassFilePath($className);
$filePath = $this->permissionChecker->getClassFilePath($className);

if (!$filePath) {
return false;
Expand Down Expand Up @@ -160,11 +167,11 @@ public function annotateDataObject($className, $undo = false)
*/
public function undoDataObject($className)
{
if (!$this->annotatePermissionChecker->classNameIsAllowed($className)) {
if (!$this->permissionChecker->classNameIsAllowed($className)) {
return false;
}

$filePath = $this->annotatePermissionChecker->getClassFilePath($className);
$filePath = $this->permissionChecker->getClassFilePath($className);

if (!$filePath) {
return false;
Expand Down Expand Up @@ -280,7 +287,7 @@ protected function generateORMOwnerProperties($className)
$owners = array();
foreach ($this->extensionClasses as $class) {
$config = Config::inst()->get($class, 'extensions', Config::UNINHERITED);
if ($config !== null && in_array($className, Config::inst()->get($class, 'extensions', Config::UNINHERITED), null)) {
if ($config !== null && in_array($className, $config, null)) {
$owners[] = $class;
}
}
Expand All @@ -292,7 +299,7 @@ protected function generateORMOwnerProperties($className)
}
$this->resultString .= $owner;
}
$this->resultString .= "|$className owner\n";
$this->resultString .= "|$className \$owner\n";
}
}

Expand Down Expand Up @@ -322,7 +329,7 @@ protected function generateORMDBProperties($className)
) {
$prop = 'float';
}
$this->resultString .= " * @property $prop $fieldName\n";
$this->resultString .= " * @property $prop \$$fieldName\n";
}
}

Expand All @@ -341,7 +348,7 @@ protected function generateORMBelongsToProperties($className)
if ($fields = Config::inst()->get($className, 'belongs_to', Config::UNINHERITED)) {
$this->resultString .= " * \n";
foreach ($fields as $fieldName => $dataObjectName) {
$this->resultString .= " * @method " . $dataObjectName . " $fieldName\n";
$this->resultString .= ' * @method ' . $dataObjectName . " \$$fieldName\n";
}
}

Expand All @@ -359,10 +366,10 @@ protected function generateORMHasOneProperties($className)
{
if ($fields = Config::inst()->get($className, 'has_one', Config::UNINHERITED)) {
foreach ($fields as $fieldName => $dataObjectName) {
$this->resultString .= " * @property int {$fieldName}ID\n";
$this->resultString .= " * @property int \${$fieldName}ID\n";
}
foreach ($fields as $fieldName => $dataObjectName) {
$this->resultString .= " * @method $dataObjectName $fieldName\n";
$this->resultString .= " * @method $dataObjectName $fieldName()\n";
}
}

Expand All @@ -381,7 +388,7 @@ protected function generateORMHasManyProperties($className)
if ($fields = Config::inst()->get($className, 'has_many', Config::UNINHERITED)) {
$this->resultString .= " * \n";
foreach ($fields as $fieldName => $dataObjectName) {
$this->resultString .= ' * @method DataList|' . $dataObjectName . "[] $fieldName\n";
$this->resultString .= ' * @method DataList|' . $dataObjectName . "[] $fieldName()\n";
}
}

Expand All @@ -399,7 +406,7 @@ protected function generateORMManyManyProperties($className)
{
if ($fields = Config::inst()->get($className, 'many_many', Config::UNINHERITED)) {
foreach ($fields as $fieldName => $dataObjectName) {
$this->resultString .= ' * @method ManyManyList|' . $dataObjectName . "[] $fieldName\n";
$this->resultString .= ' * @method ManyManyList|' . $dataObjectName . "[] $fieldName()\n";
}
}

Expand All @@ -417,7 +424,7 @@ protected function generateORMBelongsManyManyProperties($className)
{
if ($fields = Config::inst()->get($className, 'belongs_many_many', Config::UNINHERITED)) {
foreach ($fields as $fieldName => $dataObjectName) {
$this->resultString .= ' * @method ManyManyList|' . $dataObjectName . "[] $fieldName\n";
$this->resultString .= ' * @method ManyManyList|' . $dataObjectName . "[] $fieldName()\n";
}
}

Expand Down
15 changes: 10 additions & 5 deletions code/DataObjectAnnotatorTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
/**
* Class DataObjectAnnotatorTask
*
* Task to add or remove annotations from a module or dataobject
* Task to add or remove annotations from a module or dataobject.
*
* @package IDEAnnotator
*/
class DataObjectAnnotatorTask extends BuildTask
{
Expand All @@ -18,20 +20,23 @@ public function run($request)
return false;
}

$permissionHelper = Injector::inst()->get('AnnotatePermissionChecker');
$permissionChecker = Injector::inst()->get('AnnotatePermissionChecker');
$className = $request->getVar('dataobject');
$moduleName = $request->getVar('module');
$undo = $request->getVar('undo');

/* @var $annotator DataObjectAnnotator */
$annotator = DataObjectAnnotator::create();
if ($className && $permissionHelper->classNameIsAllowed($className)) {
if ($className && $permissionChecker->classNameIsAllowed($className)) {
$annotator->annotateDataObject($className, $undo);
} elseif ($moduleName && $permissionHelper->moduleIsAllowed($moduleName)) {
} elseif ($moduleName && $permissionChecker->moduleIsAllowed($moduleName)) {
$annotator->annotateModule($moduleName, $undo);
}

return null;
$result = (null !== $undo) ? "\nUndid annotating " : "\nAnnotated ";
$result .= " module $moduleName/class $className\n";

echo $result;
}

}
5 changes: 4 additions & 1 deletion code/Helpers/AnnotatePermissionChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
/**
* Class AnnotatePermissionChecker
*
* Helper to check if the class called can be annotated
* Helperclass to check if the current class or module is allowed to be annotated.
* This is abstracted from @see DataObjectAnnotator to separate and clean up.
*
* @package IDEAnnotator/Helpers
*/
class AnnotatePermissionChecker
{
Expand Down
43 changes: 25 additions & 18 deletions tests/DataObjectAnnotatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

/**
* Class DataObjectAnnotatorTest
*
* Several tests to make sure the Annotator does it's job correctly
*
* @method assertTrue() assertTrue(boolean)
* @method assertFalse() assertFalse(boolean)
* @method assertEquals() assertEquals($expected, $result)
*
* @mixin PHPUnit_Framework_TestCase
*/
class DataObjectAnnotatorTest extends SapphireTest
Expand Down Expand Up @@ -78,16 +85,16 @@ public function testFileContentWithAnnotations()
$this->assertTrue((bool)strpos($content, DataObjectAnnotator::STARTTAG));
$this->assertTrue((bool)strpos($content, DataObjectAnnotator::ENDTAG));
// database fields
$this->assertTrue((bool)strpos($content, '@property string Title'));
$this->assertTrue((bool)strpos($content, '@property int VisitCount'));
$this->assertTrue((bool)strpos($content, '@property string $Title'));
$this->assertTrue((bool)strpos($content, '@property int $VisitCount'));
// has_one ID
$this->assertTrue((bool)strpos($content, '@property int CaptainID'));
$this->assertTrue((bool)strpos($content, '@property int $CaptainID'));
// had_one relation
$this->assertTrue((bool)strpos($content, '@method DataObjectAnnotatorTest_Player Captain'));
$this->assertTrue((bool)strpos($content, '@method DataObjectAnnotatorTest_Player Captain()'));
// has_many relation
$this->assertTrue((bool)strpos($content, '@method DataList|DataObjectAnnotatorTest_SubTeam[] SubTeams'));
$this->assertTrue((bool)strpos($content, '@method DataList|DataObjectAnnotatorTest_SubTeam[] SubTeams()'));
// many_many relation
$this->assertTrue((bool)strpos($content, '@method ManyManyList|DataObjectAnnotatorTest_Player[] Players'));
$this->assertTrue((bool)strpos($content, '@method ManyManyList|DataObjectAnnotatorTest_Player[] Players()'));
// DataExtension
$this->assertTrue((bool)strpos($content, '@mixin DataObjectAnnotatorTest_Team_Extension'));
}
Expand All @@ -102,13 +109,13 @@ public function testFileContentWithoutAnnotations()

$this->assertFalse(strpos($content, DataObjectAnnotator::STARTTAG));
$this->assertFalse(strpos($content, DataObjectAnnotator::ENDTAG));
$this->assertFalse(strpos($content, '@property string Title'));
$this->assertFalse(strpos($content, '@property int VisitCount'));
$this->assertFalse(strpos($content, '@property int CaptainID'));
$this->assertFalse(strpos($content, '@method DataObjectAnnotatorTest_Player Captain'));
$this->assertFalse(strpos($content, '@method DataList|DataObjectAnnotatorTest_SubTeam[] SubTeams'));
$this->assertFalse(strpos($content, '@method ManyManyList|DataObjectAnnotatorTest_Player[] Players'));
$this->assertFalse(strpos($content, '@mixin DataObjectAnnotatorTest_Team_Extension'));
$this->assertFalse(strpos($content, '@property string $Title'));
$this->assertFalse(strpos($content, '@property int $VisitCount'));
$this->assertFalse(strpos($content, '@property int $CaptainID'));
$this->assertFalse(strpos($content, '@method DataObjectAnnotatorTest_Player Captain()'));
$this->assertFalse(strpos($content, '@method DataList|DataObjectAnnotatorTest_SubTeam[] SubTeams()'));
$this->assertFalse(strpos($content, '@method ManyManyList|DataObjectAnnotatorTest_Player[] Players()'));
$this->assertFalse(strpos($content, '@mixin DataObjectAnnotatorTest_Team_Extension()'));
}

/**
Expand Down Expand Up @@ -159,11 +166,11 @@ public function testAnnotateDataExtension()

$this->assertTrue((bool)strpos($annotated, DataObjectAnnotator::STARTTAG));
$this->assertTrue((bool)strpos($annotated, DataObjectAnnotator::ENDTAG));
$this->assertTrue((bool)strpos($annotated, '@property DataObjectAnnotatorTest_Team|DataObjectAnnotatorTest_Team_Extension owner'));
$this->assertTrue((bool)strpos($annotated, '@property string ExtendedVarcharField'));
$this->assertTrue((bool)strpos($annotated, '@property int ExtendedIntField'));
$this->assertTrue((bool)strpos($annotated, '@property int ExtendedHasOneRelationshipID'));
$this->assertTrue((bool)strpos($annotated, '@method DataObjectTest_Player ExtendedHasOneRelationship'));
$this->assertTrue((bool)strpos($annotated, '@property DataObjectAnnotatorTest_Team|DataObjectAnnotatorTest_Team_Extension $owner'));
$this->assertTrue((bool)strpos($annotated, '@property string $ExtendedVarcharField'));
$this->assertTrue((bool)strpos($annotated, '@property int $ExtendedIntField'));
$this->assertTrue((bool)strpos($annotated, '@property int $ExtendedHasOneRelationshipID'));
$this->assertTrue((bool)strpos($annotated, '@method DataObjectTest_Player ExtendedHasOneRelationship()'));
}
}

Expand Down

0 comments on commit a028c84

Please sign in to comment.