Improved support for CLI development work

1. Uses PHP_SAPI to decide CLI block should run
2. Output JSON that can be parsed by NODE.js in CLI