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 7aeb1fe + a73d2fd commit cdc0374
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 47 deletions.
6 changes: 6 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ After:
- gridfieldextensions
---

SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.ListingsCache:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: "ListingsCache"

Fromholdio\Listings\Forms\GridFieldListedPagesAddNewButton:
showEmptyString: true
5 changes: 0 additions & 5 deletions src/Extensions/ListedPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

class ListedPageExtension extends SiteTreeExtension
{
public static function add_to_class($class, $extensionClass, $args = null)
{
ListedPages::register_class($class);
}

public function updateCMSFields(FieldList $fields)
{
// Remove ParentID dropdown (if mode != cmstree)
Expand Down
5 changes: 0 additions & 5 deletions src/Extensions/ListingsRootPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ class ListingsRootPageExtension extends ListingsSiteTreeExtension
*/
private static $administration_mode = 'cmstree';

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

public function getListedPagesParentIDs()
{
return [$this->owner->ID];
Expand Down
96 changes: 77 additions & 19 deletions src/ListedPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@

use Fromholdio\CommonAncestor\CommonAncestor;
use Fromholdio\Listings\Extensions\ListedPageExtension;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Middleware\FlushMiddleware;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataList;

class ListedPages
class ListedPages implements Flushable
{
use Injectable;
use Extensible;
use Configurable;

protected static $classes = [];

public static function register_class($class)
{
self::validate_class($class);
self::$classes[$class] = $class;
}

public static function get_classes($includeSubclasses = true)
{
$classes = self::$classes;
$classes = [];

// retrieve classes from cache
$cache = self::get_cache();
$cacheKey = self::get_cache_key('Classes');
if (!$cache->has($cacheKey)) {
self::build_cache();
}
if ($cache->has($cacheKey)) {
$classes = $cache->get($cacheKey);
}

if ($includeSubclasses) {
$classes = self::add_subclasses($classes);
}

return $classes;
}

Expand All @@ -51,7 +59,7 @@ public static function get_classes_dropdown_source($includeSubclasses = true, $u

public static function get_index_classes()
{
$classes = self::$classes;
$classes = self::get_classes();
foreach ($classes as $class) {
if (!$class::singleton()->config()->get('can_be_root')) {
unset($classes[$class]);
Expand Down Expand Up @@ -133,15 +141,27 @@ public static function get_common_class($classes = null)

protected static function add_subclasses($classes)
{
$classes = array_combine($classes, $classes);

foreach ($classes as $class) {
$subclasses = ClassInfo::subclassesFor($class);
foreach ($subclasses as $subclass) {
$classes[$subclass] = $subclass;
// retrieve classes from cache
$cache = self::get_cache();
$cacheKey = self::get_cache_key('IncludeSubclasses', $classes);
if ($cache->has($cacheKey)) {

$classes = $cache->get($cacheKey);

} else {

$classes = array_combine($classes, $classes);

foreach ($classes as $class) {
$subclasses = ClassInfo::subclassesFor($class);
foreach ($subclasses as $subclass) {
$classes[$subclass] = $subclass;
}
}

$cache->set($cacheKey, $classes);
}

return $classes;
}

Expand Down Expand Up @@ -203,4 +223,42 @@ public static function validate_classes($classes)

return true;
}

/**
* 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 pages cache
$pages = [];
$classes = ClassInfo::subclassesFor(SiteTree::class);
foreach ($classes as $class) {
if ($class::has_extension(ListedPageExtension::class)) {
self::validate_class($class);
$pages[$class] = $class;
}
}
$cache = self::get_cache();
$cacheKey = self::get_cache_key('Classes');
$cache->set($cacheKey, $pages);
self::add_subclasses($pages);
}

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

private static function get_cache_key($suffix, $classes=null) {
return 'ListedPagesClasses-'.$suffix.($classes ? md5(implode('-', $classes)) : '');
}

}
95 changes: 77 additions & 18 deletions src/ListingsRoots.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,42 @@

namespace Fromholdio\Listings;

use Fromholdio\CommonAncestor\CommonAncestor;
use Fromholdio\Listings\Extensions\ListingsRootPageExtension;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Middleware\FlushMiddleware;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;

class ListingsRoots
class ListingsRoots implements Flushable
{
use Injectable;
use Extensible;
use Configurable;

protected static $classes = [];

public static function register_class($class)
{
self::validate_class($class);
self::$classes[$class] = $class;
}

public static function get_classes($includeSubclasses = true)
{
$classes = self::$classes;
$classes = [];

// retrieve classes from cache
$cache = self::get_cache();
$cacheKey = self::get_cache_key('Classes');
if (!$cache->has($cacheKey)) {
self::build_cache();
}
if ($cache->has($cacheKey)) {
$classes = $cache->get($cacheKey);
}

if ($includeSubclasses) {
$classes = self::add_subclasses($classes);
}

return $classes;
}

Expand Down Expand Up @@ -120,15 +129,28 @@ public static function get_common_plural_name($classes)

protected static function add_subclasses($classes)
{
$classes = array_combine($classes, $classes);

foreach ($classes as $class) {
$subclasses = ClassInfo::subclassesFor($class);
foreach ($subclasses as $subclass) {
$classes[$subclass] = $subclass;
$cache = self::get_cache();

// retrieve classes from cache
$cacheKey = self::get_cache_key('IncludeSubclasses', $classes);
if ($cache->has($cacheKey)) {

$classes = $cache->get($cacheKey);

} else {

$classes = array_combine($classes, $classes);

foreach ($classes as $class) {
$subclasses = ClassInfo::subclassesFor($class);
foreach ($subclasses as $subclass) {
$classes[$subclass] = $subclass;
}
}

$cache->set($cacheKey, $classes);
}

return $classes;
}

Expand Down Expand Up @@ -190,4 +212,41 @@ public static function validate_classes($classes)

return true;
}

/**
* 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 pages cache
$pages = [];
$classes = ClassInfo::subclassesFor(SiteTree::class);
foreach ($classes as $class) {
if ($class::has_extension(ListingsRootPageExtension::class)) {
self::validate_class($class);
$pages[$class] = $class;
}
}
$cache = self::get_cache();
$cacheKey = self::get_cache_key('Classes');
$cache->set($cacheKey, $pages);
self::add_subclasses($pages);
}

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

private static function get_cache_key($suffix, $classes=null) {
return 'ListingsRootClasses-'.$suffix.($classes ? md5(implode('-', $classes)) : '');
}
}

0 comments on commit cdc0374

Please sign in to comment.