From 01c483932879e3b0e5d5ace527cab59868875c89 Mon Sep 17 00:00:00 2001 From: Simon Erkelens Date: Mon, 4 Sep 2023 12:38:22 +1200 Subject: [PATCH] Read the DBField tagnames from config (#153) --- README.md | 4 ++-- _config/config.yml | 5 +++++ docs/en/Installation.md | 7 +++++++ src/DataObjectAnnotator.php | 19 +++++++++++++++++++ src/Generators/OrmTagGenerator.php | 27 ++++++--------------------- 5 files changed, 39 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 9aa89e6..4b9aecc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/_config/config.yml b/_config/config.yml index 44e2655..34175ba 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -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 diff --git a/docs/en/Installation.md b/docs/en/Installation.md index e668fba..4627187 100644 --- a/docs/en/Installation.md +++ b/docs/en/Installation.md @@ -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. diff --git a/src/DataObjectAnnotator.php b/src/DataObjectAnnotator.php index 0de2327..5f319e3 100644 --- a/src/DataObjectAnnotator.php +++ b/src/DataObjectAnnotator.php @@ -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; /** @@ -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. * diff --git a/src/Generators/OrmTagGenerator.php b/src/Generators/OrmTagGenerator.php index e4bb5c3..5de1f38 100644 --- a/src/Generators/OrmTagGenerator.php +++ b/src/Generators/OrmTagGenerator.php @@ -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; /** @@ -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 */ @@ -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) { @@ -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}()";