Skip to content

Commit

Permalink
Read the DBField tagnames from config (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firesphere authored Sep 4, 2023
1 parent 62b2d07 commit 01c4839
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ The docblocks can be generated/updated with each dev/build and with a DataObject

SilverStripe Framework and possible custom code.

By default, `mysite` is an enabled "module".
By default, `mysite` and `app` are enabled "modules".

### Version ^2:
SilverStripe 3.x framework

### Version ^3:
SilverStripe 4.x
SilverStripe 4.x+

## Installation

Expand Down
5 changes: 5 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ SilverLeague\IDEAnnotator\DataObjectAnnotator:
enabled_modules:
- mysite
- app
dbfield_tagnames:
SilverStripe\ORM\FieldType\DBInt: int
SilverStripe\ORM\FieldType\DBBoolean: bool
SilverStripe\ORM\FieldType\DBFloat: float
SilverStripe\ORM\FieldType\DBDecimal: float
7 changes: 7 additions & 0 deletions docs/en/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ SilverLeague\IDEAnnotator\DataObjectAnnotator:
- mysite
```
If you want to add extra field types that do not return one of the known values, you can add it as such:
```yaml
SilverLeague\IDEAnnotator\DataObjectAnnotator:
dbfield_tagnames:
Symbiote\MultiValueField\ORM\FieldType\MultiValueField: 'MultiValueField|string[]'
```
**NOTE**
- Using short names, will also shorten core names like `ManyManyList`, you'll have to adjust your use statements to work.
Expand Down
19 changes: 19 additions & 0 deletions src/DataObjectAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBBoolean;
use SilverStripe\ORM\FieldType\DBDecimal;
use SilverStripe\ORM\FieldType\DBFloat;
use SilverStripe\ORM\FieldType\DBInt;
use stdClass;

/**
Expand Down Expand Up @@ -75,6 +79,21 @@ class DataObjectAnnotator
*/
private $annotatableClasses = [];

/**
* Default tagname will be @string .
* All exceptions for @see DBField types are listed here
*
* @see generateDBTags();
* @config Can be overridden via config
* @var array
*/
protected static $dbfield_tagnames = [
DBInt::class => 'int',
DBBoolean::class => 'bool',
DBFloat::class => 'float',
DBDecimal::class => 'float',
];

/**
* DataObjectAnnotator constructor.
*
Expand Down
27 changes: 6 additions & 21 deletions src/Generators/OrmTagGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace SilverLeague\IDEAnnotator\Generators;

use SilverLeague\IDEAnnotator\DataObjectAnnotator;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBBoolean;
use SilverStripe\ORM\FieldType\DBDecimal;
use SilverStripe\ORM\FieldType\DBFloat;
use SilverStripe\ORM\FieldType\DBInt;
use SilverStripe\ORM\ManyManyList;

/**
Expand All @@ -35,20 +32,6 @@ class OrmTagGenerator extends AbstractTagGenerator
'Extensions',
];

/**
* Default tagname is will be @string .
* All exceptions for @see DBField types are listed here
*
* @see generateDBTags();
* @var array
*/
protected static $dbfield_tagnames = [
DBInt::class => 'int',
DBBoolean::class => 'bool',
DBFloat::class => 'float',
DBDecimal::class => 'float',
];

/**
* Generates all ORM Tags
*/
Expand Down Expand Up @@ -81,7 +64,9 @@ public function getTagNameForDBField($dbFieldType)
// some fields in 3rd-party modules require a name...
$fieldObj = Injector::inst()->create($dbFieldType, 'DummyName');

foreach (self::$dbfield_tagnames as $dbClass => $tagName) {
$fieldNames = DataObjectAnnotator::config()->get('dbfield_tagnames');

foreach ($fieldNames as $dbClass => $tagName) {
if (class_exists($dbClass)) {
$obj = Injector::inst()->create($dbClass);
if ($fieldObj instanceof $obj) {
Expand Down Expand Up @@ -128,11 +113,11 @@ protected function generateHasOneTags()
if ($fields = (array)$this->getClassConfig('has_one')) {
foreach ($fields as $fieldName => $dataObjectName) {
$this->pushPropertyTag("int \${$fieldName}ID");

if ($dataObjectName === DataObject::class) {
$this->pushPropertyTag("string \${$fieldName}Class");
}

$dataObjectName = $this->getAnnotationClassName($dataObjectName);
$tagString = "{$dataObjectName} {$fieldName}()";

Expand Down

0 comments on commit 01c4839

Please sign in to comment.