Exploratory concepts¶
Actions that store nothing¶
An operation reads or writes what a resource stores. Some of core's forms do neither. They run something, and take little or no input:
| Core's form | What it does |
|---|---|
| "Clear all caches", on the performance page | Rebuilds the site's caches. It takes no input |
"Flush", for an image style (/admin/config/media/image-styles/manage/{image_style}/flush) |
Deletes the images generated for that style. It needs to know which style, and changes no stored value |
| "Run cron", on the cron settings form | Runs cron. It takes no input |
Each has a permission today, and each is worth running from Drush, HTTP or an agent with the same permission and checks. None has values to describe, load or save, so the two kinds of operation on the rest of this site do not fit them.
Tool API's operations¶
Tool API already names this difference. Every Tool API tool states its operation, which says what kind of action it is:
| Tool API's operation | What it means | What it could be here |
|---|---|---|
| Read | Returns stored data | An operation on a resource |
| Write | Creates, updates or deletes stored data | An operation on a resource |
| Explain | Describes a definition or a schema | Mostly nothing to write: every operation serves its schema |
| Trigger | Starts a process, such as cron | An attribute of its own |
| Transform | Returns a result made from its input, and changes nothing | An attribute of its own |
The word means something different in each. On this site an operation is the action itself. In Tool API it is the kind of action a tool performs.
Trigger and Transform as attributes¶
Tool API states the kind as one value on the tool, and the same tool class holds the input, the access check and the work. The idea is to give each kind its own attribute instead. Read and Write are covered by a resource's operations, and Explain mostly by the schema, which leaves two:
- Trigger. Does something other than save stored values: rebuild the caches, flush an image style, run cron.
- Transform. Takes input, returns a result and changes nothing: the text that a text format produces from some input.
Each would go on the method that does the work, as #[Hook] does, in a class that can have services injected. This one stands alone:
final class CacheTriggers {
#[Trigger('cache.rebuild',
label: new TranslatableMarkup('Clear all caches'),
permission: 'administer site configuration',
)]
public function rebuild(): void {
drupal_flush_all_caches();
}
}
A resource could have them too, beside its operations. This sketch is of a resource for image styles, which the proof of concept does not have. The trigger is on the resource's own class, so it does not name the resource:
#[Resource('image.style', identity: ['name'], storage: ImageStyleStorage::class)]
final class ImageStyleResource implements ResourceInterface {
public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager,
) {}
#[Operation('update',
label: new TranslatableMarkup('Edit an image style'),
permission: 'administer image styles',
)]
public static function update(string $name): OperationContext {
return new OperationContext(known: ['name' => $name]);
}
#[Trigger('flush',
label: new TranslatableMarkup('Flush'),
permission: 'administer image styles',
)]
public function flush(string $name): void {
$this->entityTypeManager->getStorage('image_style')->load($name)?->flush();
}
// The keys are left out.
}
A trigger in another class, such as another module's, would name the resource it belongs to.
- Identity, and no stored values. A trigger on a resource is told which thing it is about. It is not given what that thing stores, which an update is.
- Static methods declare, other methods do the work. An operation's method is static and says which thing is meant, because storage does its work. A trigger's method is the work.
- The same ways in. A form, HTTP, the OpenAPI document and Drush would run one as they run an operation. For the two above, the form is a button or a confirmation.
- Dry run. A trigger could name a method that checks and changes nothing. Without one, a dry run can only check the permission and the input, and should say so. A transform changes nothing, so running it is the dry run.
What a tool would then be¶
With the action described on its own, a tool has less to hold. It would present an operation, a trigger or a transform to a caller such as an agent, with a name and a description written for that caller. It could offer part of one, with some values fixed, or split a large one into smaller tools. The permission, the validation and the work would stay with the action, so a tool could limit what a caller sends and never allow more.
A resource stays one to one with an API: one thing, at one address, with its operations.
TBD
- Whether Trigger and Transform are worth two more attributes, or one is enough.
- Where each is declared, and how an action with no resource is listed and addressed over HTTP.
- How a method written for other PHP code is made safe to call from outside: its input checked before it runs, and a refusal a caller can read.
- How this meets Tool API's operations, which its tools use today.