What is the best way to use DomainSession in a command line environment?

Jory Geerts asks:

Do you have any thoughts on how [DomainSession] could be used from a CLI environment?

The first thing to ask yourself is how would you like sessions to work on the command line? There are a few ways this could work.

Each run has its own new session

The main reason I see for this option would be to reuse the domain logic from your web app and it depends on DomainSession. You don’t really care what gets stored in the session because you consider it temporary.

In bin/with_new.php I’ve defined a very simple script.

First, I init the script. In this case, since I’m using a Radar application, I just use Aura.Di directly to load DomainModule. This makes sure I have SessionManager and Home configured.

If I was doing this in production, I’d probably have a different definition for DomainModule that I used with the CLI that used the Memory storage instead of File storage. In this example, I won’t worry about that.

Then I can get a new instance of my domain object Home and call it with null as the $sessionId. This will let the SessionManager generate a new ID for this request.

Each run has its own new prepopulated session

If I need to prepopulate the session with data (like a logged in user) I can simply start a new session, assign the values I want to the session and finish it like normal. Then I can grab the session ID from that session and pass it into the domain object so it can reuse that session.

I implement this in bin/with_prepopulated.php.

Reuse the session between script runs

The next option would be if you wanted to reuse the session between script runs. You’ll need to determine how you want to store the $sessionId between script runs, but one way would be to print it in the output, then include it as a parameter when calling your script in the future.

I implement this in bin/with_reused.php.

The output would work as such:


These three ways illustrate some of the ways you could to go about solving this issue. There will most likely be specific requirements based on your application, but it will most likely be some variation of these. For instance instead of printing the $sessionId you might store it in a file.

What do you think? Let me know in the comments if you see any problems with these approaches.

Author: Andrew Shell

Madison, WI developer, Co-Founder and CTO of Pinpoint Software, founder of Madison PHP.

2 thoughts on “What is the best way to use DomainSession in a command line environment?”

  1. Sessions do not make sense unless you have an HTTP request. If you needed sessions under the command-line, that points at a dependency/architectural issue – I’d say fix that rather than struggling to come up with a workaround.

    1. I can see your point, but only because sessions are traditionally implemented with cookies. The premise of DomainSession is that the session handling is a domain responsibility. The domain doesn’t know or care how it’s being accessed by the user. If you’re accessing your domain via HTTP, then using cookies is probably the best idea. All the domain cares about is getting an id to look up an existing session, and returning the id so future requests could share the data.

Leave a Reply