Jump to content


Photo

Re-purposing web based service code for standalone (command-line) interface.


  • Please log in to reply
1 reply to this topic

#1 karlkras

karlkras
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 07 December 2017 - 03:46 PM

Overview: 

I'm trying to determine the best way to reuse back-end business modules in an existing web application.

The UI for this application is pretty straight forward:

  1. Form on first page collects a handful of options,
  2. on submit it gets stuffed into the session and
  3. the target process takes this information, does some processing using this information (calls external services, retrieves database information, etc.) and conditionally generates some business objects,
  4. stuffs this into the session and then
  5. passes this along to the centralized back-end driver.
  6. This driver now accesses the objects in the session for initialization purposes. So currently this driver is dependent on the web session service for providing these items.

I want to re-purpose this driver for a command-line interface. So these options from the initial web page can instead be passed in as args to an entry point that then performs the steps from 3 on.

 

Ideally I would like to use the same modules for both the web and standalone applications in order to avoid code duplication, but the dependency on the web session object, and how to decouple this is a head scratcher.

 

I was hoping I might be able to "spoof" a web session in the standalone version, but if not running within the context of an http client I'm pretty sure this isn't possible?

 

Outside of this I was thinking that I would have to pull session stuff out of the code entirely and using some other mechanism for passing data around, this wouldn't be an easy task, lots of modules would have to touched as a result.

 

Other then generating an entirely separate branch of code (which I'm afraid I'm pretty much going to have to do) or a wholesale restructuring of the code base, any suggestions on any relatively lightweight alternatives?

 

thanks!



#2 kicken

kicken
  • Gurus
  • Wiser? Not exactly.
  • 3,332 posts
  • LocationBonita, FL

Posted 08 December 2017 - 05:44 PM

Steps 4 and 5 seem unncessary if you're going to have everthing from 3 - 6 be part of one CLI process. You don't need to stuff the data into something then pull it back out, just pass it around as arguments. If it's a lot of data, maybe make an object to represent it and pass that along.

Since I have no idea what kind of data/processes you're dealing with, in a highly abstract way it's just look something like:
function step3Stuff($options){
    //...
    return $newObjects;
}

function step6Stuff($objects){
    //...
}

$options = $_SERVER['argv'][1];
$objects = step3Stuff($options);
step6Stuff($objects);
Unless the code is messy/unorganized it shouldn't require that much effor to change the bits that grab from the session into bits that grab from parameters or similar.


That said, you could just accept the data via the command line then stuff it into $_SESSION yourself, such as:
$_SESSION['foo'] = $_SERVER['argv'][1];
$_SESSION['bar'] = $_SERVER['argv'][2];

doStuff();
I wouldn't recommend that approach though as it's going to create some confusing code/contexts.
Did I help you out? Feeling generous? I accept tips via Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7
Kicken's World⦄ ⦃Recycle old CD's




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users