Where (namespace+directory) should my application level custom console command go?

Hi all,

I’m trying to define a custom command to run from vendor/bin/concrete . I’m trying to register it from bootstrap.app as:
if ($console) {
$console->add($app->make(MyCommand::class)
}

But I can’t figure out where to put my command class and what namespace to give it so that Concrete’s autoloader will find it. My guess was public/application/console/Command (namespace Application\Console\Command) or maybe application/src/Console/Command (namespace Application\Src\Console\Command) and I tried lots of other variations but make() simply won’t find it. From debug-stepping through the autoloader (specifically ClassAutoLoader::loadClassFromApplication) it seems you can only use specific directories in the application dir (like controllers, blocks etc) and console isn’t one of them.

I also tried putting it in the root /src of my project but then I have even less of a clue what namespace to give it, Composer\Concrete5 like in src/helpers.php didn’t work. :slight_smile:

Any help would be appreciated :slight_smile:

Maurits.

Replying to myself here cause I think I figured it out. :slight_smile:

So I think there’s no predefined place within Concrete to add your own console commands. Which means it doesn’t go in application/, and therefore the root /src is a nice place. However, autoload is not configured to find anything else than the provided src/helpers.php. So instead I added an extra namespace definition in composer.json:

"autoload": { "psr-4": { "ConcreteComposer\\": "./src", "Application\\": "./src/app" },

And now anything I put in the src/app directory (like src/app/Console/Command) will end up in the Application namespace (Application\Console\Command). This will work nicely too for other additions outside the Concrete structure. As far as I can tell, existing things in the Application\ namespace still work fine too.

This is an interesting solution. I’ve never attempted this outside a package…when i do it in package i always put them in [package]/src/Command

Concrete is able to autoload classes located in the <webroot>/application/src directory without any need to add custom autoload locations.

In particular, classes must start with the Application\Concrete namespace, which is mapped to the <webroot>/application/src/Concrete directory.
You can also define Doctrine entities in the Application\Entity namespace, which is mapped to the <webroot>/application/src/Entity directory.

For example, you can define a Application\Concrete\Console\Command\MyCommand class, stored in the <webroot>/application/src/Concrete/Console/Command/MyCommand.php file.

1 Like