Skip to content

Commit

Permalink
Merge pull request #1 from xini/master
Browse files Browse the repository at this point in the history
move registration of classes to dev/build using cache
  • Loading branch information
dizzystuff authored Jul 24, 2022
2 parents d112619 + 88516f4 commit 0a1ec6e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 36 deletions.
6 changes: 6 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
---
Name: fromholdio-attributable
---

SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.AttributionCache:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: "AttributionCache"
11 changes: 0 additions & 11 deletions src/Extensions/Attributable.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ class Attributable extends DataExtension
'Attributions'
];

public static function add_to_class($class, $extensionClass, $args = null)
{
$classInst = $class::singleton();
if ($classInst->hasMethod('isAttributeFilterOnly')) {
if ($classInst->isAttributeFilterOnly()) {
return;
}
}
Attribution::register_object($class);
}

public function getAttributes($attrClass, $scopeObject = null)
{
if (!$this->owner->ID) {
Expand Down
5 changes: 0 additions & 5 deletions src/Extensions/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ class Attribute extends DataExtension
'AttrAttributions' => Attribution::class . '.Attribute'
];

public static function add_to_class($class, $extensionClass, $args = null)
{
Attribution::register_attribute($class);
}

public function updateCMSFields(FieldList $fields)
{
$fields->removeByName('AttrAttributions');
Expand Down
104 changes: 84 additions & 20 deletions src/Model/Attribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

use Fromholdio\Attributable\Extensions\Attributable;
use Fromholdio\Attributable\Extensions\Attribute;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Control\Middleware\FlushMiddleware;
use Fromholdio\CommonAncestor\CommonAncestor;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\Versioned\Versioned;

class Attribution extends DataObject
class Attribution extends DataObject implements Flushable
{
private static $table_name = 'Attribution';
private static $singular_name = 'Attribution';
Expand All @@ -30,25 +34,18 @@ class Attribution extends DataObject
'Object' => DataObject::class
];

protected static $attributes = [];
protected static $objects = [];

public static function register_attribute($class)
{
self::validate_attribute($class);
self::$attributes[$class] = $class;
}

public static function register_object($class)
{
self::validate_object($class);
self::$objects[$class] = $class;
}

public static function get_attributes()
{
$classes = self::$attributes;
return $classes;
// retrieve from cache
$cache = self::get_cache();
$cacheKey = self::get_attributes_cache_key();
if (!$cache->has($cacheKey)) {
self::build_cache();
}
if ($cache->has($cacheKey)) {
return $cache->get($cacheKey);
}
return null;
}

public static function get_attributes_for_dropdown($usePlural = false)
Expand Down Expand Up @@ -81,8 +78,16 @@ public static function get_attribute_by_url_segment($urlSegment)

public static function get_objects()
{
$classes = self::$objects;
return $classes;
// retrieve from cache
$cache = self::get_cache();
$cacheKey = self::get_objects_cache_key();
if (!$cache->has($cacheKey)) {
self::build_cache();
}
if ($cache->has($cacheKey)) {
return $cache->get($cacheKey);
}
return null;
}

public static function validate_object($class)
Expand Down Expand Up @@ -190,6 +195,65 @@ public function onBeforeWrite()
$this->AttributeKey = $this->AttributeClass . '|' . $this->AttributeID;
$this->ObjectKey = $this->ObjectClass . '|' . $this->ObjectID;
}

/**
* This function is triggered early in the request if the "flush" query
* parameter has been set. Each class that implements Flushable implements
* this function which looks after it's own specific flushing functionality.
*
* @see FlushMiddleware
*/
public static function flush()
{
self::get_cache()->clear();
self::build_cache();
}

private static function build_cache() {
// build attributes cache
$attributes = [];
$classes = ClassInfo::subclassesFor(DataObject::class);
foreach ($classes as $class) {
if ($class::has_extension(Attribute::class)) {
self::validate_attribute($class);
$attributes[$class] = $class;
}
}
$cache = self::get_cache();
$cacheKey = self::get_attributes_cache_key();
$cache->set($cacheKey, $attributes);

// build objects cache
$objects = [];
$classes = ClassInfo::subclassesFor(DataObject::class);
foreach ($classes as $class) {
if ($class::has_extension(Attributable::class)) {
$classInst = $class::singleton();
if ($classInst->hasMethod('isAttributeFilterOnly')) {
if ($classInst->isAttributeFilterOnly()) {
break;
}
}
self::validate_object($class);
$objects[$class] = $class;
}
}
$cache = self::get_cache();
$cacheKey = self::get_objects_cache_key();
$cache->set($cacheKey, $objects);
}

private static function get_cache() {
return Injector::inst()->get(CacheInterface::class . '.AttributionCache');
}

private static function get_attributes_cache_key() {
return implode('-', ['Attributes']);
}

private static function get_objects_cache_key() {
return implode('-', ['Objects']);
}

public static function get_related_objects($attrClassName, array $attrIDs, array $objClassNames)
{
Expand Down

0 comments on commit 0a1ec6e

Please sign in to comment.