karlkras Posted December 7, 2017 Share Posted December 7, 2017 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: Form on first page collects a handful of options, on submit it gets stuffed into the session and 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, stuffs this into the session and then passes this along to the centralized back-end driver. 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! Quote Link to comment https://forums.phpfreaks.com/topic/305864-re-purposing-web-based-service-code-for-standalone-command-line-interface/ Share on other sites More sharing options...
kicken Posted December 8, 2017 Share Posted December 8, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305864-re-purposing-web-based-service-code-for-standalone-command-line-interface/#findComment-1554550 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.