3 CLI Frameworks
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Introduction
Web frameworks like Symfony and Laravel ship with a sub-framework that helps the developer add command-line-based tasks to their projects. Usually these tasks are implemented as “commands”, which are classes that extend from some kind of abstract Command class. Out of the box a project already has some commands available, e.g. for migrating the database schema, or for setting up a new controller. When developers start adding their own commands to the project, those commands will be in one way or another coupled to the command-line-interface (CLI) framework of their project.
Decoupling from a CLI framework isn’t always necessary, but it will certainly give you an advantage when the time comes to upgrade or switch. In that case, there will be several areas where work is needed:
- In the code that defines the available command-line options and arguments supported by the command
- In the code that reads the user-provided options and arguments based on these definitions
- In the code that deals with the output streams (
stdoutandstderr)
Each framework will offer its own API for that, so these are the areas where we’ll have to rewrite existing code. To make this easier for us, extracting input and producing output is something that should only happen in a few places in our codebase. If this is the case, there will be a large chunk of the code that doesn’t have to be adapted when switching to a different CLI framework.
Input
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Collect Input First
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Jump to a Service
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Output
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Using an Observer for Showing Output
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Generalizing the Solution with Event Dispatching
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Turn the Command Class Itself Into an Event Subscriber
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Controllers Should Call Framework-agnostic Services
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Rules for Decoupling
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.
Use InputInterface and OutputInterface Only in Command Classes
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/recipes-for-decoupling.