How to use middleware for core shell commands

I’ve got a plugin in cakephp that runs first whenever I execute the command migrations migrate from the CLI.

I want to add an argument, say cake migrations migrate -v=123 that will be used by this plugin and then remove it from the shell command before the command reaches the main migrations plugin.

Currently, I’m trying this via a plugin that has a bootstrap file which modifies the global variable args, grabbing the argument and then removing it from there, the issue is that, even if I modify this variable or the $_SERVER[‘args’] the original list of args still reaches the migrations plugin and then it throws an error because of the extra argument.

I’m thinking there would be 2 ways:

  • Get the argument and then remove it from the argument list(Here I don’t know how to overwrite the argument list so the migrations plugins gets the updated args list)
  • Stop the shell execution within this plugin, get the argument and then start another shell execution without the argument.(Here I dont know how to stop the shell execution within the plugin’s bootstrap since I dont have a shell instance to use abort)

Any ideas how I could accomplish either way of those methods? Consequently, if you have any other suggestions, would love to hear them

I’m not sure about the exact solution to your problem because I don’t know the differences between a regular request startup process and a command line startup.

However, here is a diagram of the regular request startup. CakePHP 4.x Request/Resonse Sequence Overview

Step 8 is where you can get involved in both Cake and Plugin bootstrap processes. Step 11 is where you can operate on both Cake and Plugin Middleware initialization.

So, Application.php might be your only tool to effect things so early. After those two calls, things are mostly underway.

That’s very useful information, I’ve got some suggestions on the slack channel to actually use my own shell command where I get the parameter and do whatever I want with it and then dispatch a new command.

Which works fine now, except that I’m creating a new datasource in my command and it seems that I can’t use it after that at all.

An exception is being thrown:

 Datasource class poc_tst could not be found.  in [/var/www/.../migrations/vendor/cakephp/cakephp/src/Datasource/ConnectionRegistry.php, line 57]

which doesn’t really make sense to me since I just created the new datasource, I used debug to check that its there and it is

I’ve been having a bit of trouble swapping between datasources too (in a cake app). I’ve got some details to learn there.