Karmabunny / sprout3

SproutCMS: content management and framework
http://getsproutcms.com
GNU General Public License v2.0
24 stars 3 forks source link

Feat/unified controller names #75

Closed gwillz closed 7 months ago

gwillz commented 1 year ago

This is part of Sprout 4.0

Sprout admin controllers have a surprisingly long history. Over time they have accumulated some cruft, sharp edges, and their behaviours can be - at times - perplexing. This PR attempts to resolve some of that ambiguity.

First some basics:

Some issues:

Unification of short names

So $controller_name still exists, but it's no longer required to specify when creating a controller. If empty it will populate from the register. All short names can now be declared in one place (the register). This also means all core controllers are now registered (more on this later).

Content controllers

There was a weak description of a 'content controller' - one that permits per-record and per-subsite permissions. This is loosely defined by Register::coreContentControllers() and some inheritance filters. All module controllers are implicitly a content controller.

So content controllers were:

We now have a clear filtering system. Controllers have a _getContentsPermissionGroups() helper that returns boolean map to inform the permission controllers if they have these features enables.

URL discovery

The process was this:

  1. Look up a controller class using it's registered short name
  2. Convert short name: snake_case -> CamelCase -> plural -> + AdminController -> + namespace
  3. Reflection errors if it's missing

It may make sense, but it can be a surprising behaviour considering how modules and URL creation is performed.

Having resolved categorisation of 'content controllers' means all core controllers can be registered and their short names are also in one place. We now have an admin_load.php in the sprout app folder to achieve this.

The process is now:

  1. Look up a controller class using it's registered short name
  2. That's it.

Some perks

Controller registration doesn't need to conform to the SproutModules\Author\Module\Controllers\Admin\ pattern. This was required to properly register the core controllers. However this same functionality was required for the upcoming external modules support - so yay.

Friendly names, navigation names, table names - these are all now generated on construct (if empty). Their behaviour generally is also now a little more consistent. I would like to make these static so we don't have to do stupid reflection things all over the place.

Category controllers will naturally create all their own handles/names based on the expected structures. A basic category controller can be declared like this:

RedirectCategoryAdminController extends CategoryAdminController {}

Related future work

JsonForms have always bothered me. Too much of their logic lives in the base controller class - something that isn't relevant to frontend controllers. This code should live in the ManagedAdminController class or a different helper, possibly a trait. An external helper poses challenges because how tightly the action log and table_name is tied into the controller classes.

Might still rework name helpers into static methods.