Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit 4ff010f66437e04a98a93469fce9ce3ac03f14b2 0 parents
@eddieajau authored
2  .gitignore
@@ -0,0 +1,2 @@
+/.*
+/config/config.json
66 README.md
@@ -0,0 +1,66 @@
+# Bach - Your basic command line application
+
+Like his [Minuet in G](http://www.youtube.com/watch?v=on1DDSLdDOo) (incidentally, the first classical piece I learned on the piano), **Bach** is a very simple skeleton for a command line application. It demonstrates:
+
+* how to bootstrap the Joomla Platform;
+* how to provide a configuration file for an application;
+* how to set up the application source code to the auto-loader specifications;
+* how to identify command line options; and
+* how to use a model and its state.
+
+## Overview
+
+<dl>
+ <dt>bin/</dt>
+ <dd>This folder contains the executatable files.</dd>
+ <dt>code/</dt>
+ <dd>This folder contains all the custom application code.</dd>
+ <dt>config/</dt>
+ <dd>This folder contains the configuration file for the application.</dd>
+</dl>
+
+Note that within the ``code/`` folder, all the classes are prefixed with "Bach" and then all the classes follow the auto-loader naming convention. The prefix is specified in the last line of the ``code/bootstrap.php`` file:
+
+<pre>// Setup the autoloader for the Bach application classes.
+JLoader::registerPrefix('Bach', __DIR__);</pre>
+
+You can search-and-replace the word "Bach" and replace it with your own application name. You can also search-and-replace "{COPYRIGHT}".
+
+This type of architecture could be used to support a single command-line application, or it could be used for many different variants, each with its own executable in ``bin/``, but using a common code library under ``code/``.
+
+## Requirements
+
+* PHP 5.3+
+
+## Installation
+
+This application assumes that you have cloned it, and the Joomla Platform (you need to use [eBay's fork](https://github.com/eBaySF/joomla-platform/tree/mvc) for now because this example uses the new proposed MVC) into a folder called "joomla" under the same parent. For example:
+
+<pre>/parent
+../Bach &lt;-- this repository
+../joomla &lt;-- eBay's platform</pre>
+
+The simplest way to do this is like this:
+
+<pre>mkdir Composers
+cd Composers
+git clone git://github.com/ebaysf/joomla-platform.git joomla
+git clone git://github.com/eddieajau/jc-bach.git Bach</pre>
+
+Such a setup will allow the application to auto-discover the Joomla Platform. Alternatively, you can configure some environment variables so that your applications know where to find the Joomla Platform (probably the way you would do it on the production server).
+
+<pre>JPLATFORM_HOME=/path/to/joomla/platform</pre>
+
+Once you have cloned this repository, you can then push it to one your have created for your own application.
+
+## Running the application
+
+To run the application, navigation to the "/bin" folder and make sure the "run" file is executable. Then run it directly (if on a *nix operating system), or by invoking php like this (you loose the ability to use command line arguments for the script itself doing it this way):
+
+<pre>php -f run</pre>
+
+Consult your operating system requirements for putting the script in your execution path so that you can run it from any folder.
+
+## About the Joomla Composers
+
+The Joomla Composers are a suite of skeleton git repositories that can be used to kick-start your own Joomla Platform Projects. You can clone any of the repositories as a base for building your own application, or cherry-pick what you need.
43 bin/run
@@ -0,0 +1,43 @@
+#!/usr/bin/php
+<?php
+/**
+ * Command line script to run the Bach Application.
+ *
+ * @package Bach.Application
+ *
+ * @copyright Copyright (C) {COPYRIGHT}. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+// Bootstrap the application.
+$path = getenv('BACH_HOME');
+if ($path)
+{
+ require_once $path . '/bootstrap.php';
+}
+else
+{
+ require_once realpath(__DIR__ . '/../code/bootstrap.php');
+}
+
+// Set all loggers to echo.
+JLog::addLogger(array('logger' => 'echo'), JLog::ALL);
+
+// Instantiate the application.
+$application = JApplicationCli::getInstance('BachApplicationCli');
+
+// Store the application.
+JFactory::$application = $application;
+
+// Wrap the execution in a try statement to catch any exceptions thrown anywhere in the script.
+try
+{
+ // Execute the application.
+ $application->execute();
+}
+catch (Exception $e)
+{
+ // An exception has been caught, just echo the message.
+ fwrite(STDOUT, $e->getMessage() . "\n");
+ exit($e->getCode());
+}
131 code/application/cli.php
@@ -0,0 +1,131 @@
+<?php
+/**
+ * @package Bach.Application
+ * @subpackage Application
+ *
+ * @copyright Copyright (C) {COPYRIGHT}. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+/**
+ * Bach CLI Application Class
+ *
+ * @package Bach.Application
+ * @subpackage Application
+ * @since 1.0
+ */
+class BachApplicationCli extends JApplicationCli
+{
+ /**
+ * Execute the application.
+ *
+ * @return void
+ *
+ * @since 1.0
+ */
+ public function doExecute()
+ {
+ // Check if help is needed.
+ if ($this->input->get('h', $this->input->get('help')))
+ {
+ $this->_help();
+
+ return;
+ }
+
+ // Check command line switches.
+ $tzLocal = $this->input->get('l') || $this->input->get('local');
+ $tzUtc = $this->input->get('u') || $this->input->get('utc');
+
+ // Create a registry for the model state.
+ $state = new JRegistry;
+
+ // Set the timezone state for the model.
+ if ($tzLocal)
+ {
+ $state->set('timezone', $this->config->get('timezone'));
+ }
+ elseif ($tzUtc)
+ {
+ $state->set('timezone', 'utc');
+ }
+
+ $model = new BachModelDefault($state);
+ $date = $model->getTime();
+
+ $this->out(sprintf('This time is %s', $date->toRFC822(true)));
+ $this->out(sprintf('This timezone is %s', $date->getTimezone()->getName()));
+ }
+
+ /**
+ * Fetch the configuration data for the application.
+ *
+ * @return object An object to be loaded into the application configuration.
+ *
+ * @since 1.0
+ * @throws RuntimeException if file cannot be read.
+ */
+ protected function fetchConfigurationData()
+ {
+ // Initialise variables.
+ $config = array();
+
+ // Ensure that required path constants are defined.
+ if (!defined('JPATH_CONFIGURATION'))
+ {
+ $path = getenv('BACH_CONFIG');
+ if ($path)
+ {
+ define('JPATH_CONFIGURATION', realpath($path));
+ }
+ else
+ {
+ define('JPATH_CONFIGURATION', realpath(dirname(JPATH_BASE) . '/config'));
+ }
+ }
+
+ // Set the configuration file path for the application.
+ if (file_exists(JPATH_CONFIGURATION . '/config.json'))
+ {
+ $file = JPATH_CONFIGURATION . '/config.json';
+ }
+ else
+ {
+ // Default to the distribution configuration.
+ $file = JPATH_CONFIGURATION . '/config.dist.json';
+ }
+
+ if (!is_readable($file))
+ {
+ throw new RuntimeException('Configuration file does not exist or is unreadable.');
+ }
+
+ // Load the configuration file into an object.
+ $config = json_decode(file_get_contents($file));
+
+ return $config;
+ }
+
+ /**
+ * Display the help text for the command line application.
+ *
+ * @return void
+ *
+ * @since 1.0
+ */
+ private function _help()
+ {
+ $this->out('Bach 1.0.');
+ $this->out();
+ $this->out('Tells the server, local or UTC time.');
+ $this->out();
+ $this->out('Usage: run [switches]');
+ $this->out();
+ $this->out(' -h | --help Prints this usage information.');
+ $this->out(' -l | --local Gives the time according to the local configuration setting.');
+ $this->out(' -u | --utc Gives the time in UTC.');
+ $this->out();
+ $this->out('Examples: ./run -h');
+ $this->out();
+ }
+}
51 code/bootstrap.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * Bootstrap file for the Bach Application.
+ *
+ * @package Bach.Application
+ *
+ * @copyright Copyright (C) {COPYRIGHT}. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+// Set the Joomla execution flag.
+define('_JEXEC', 1);
+
+// Allow the application to run as long as is necessary.
+ini_set('max_execution_time', 0);
+
+// Note, you would not use these settings in production.
+error_reporting(E_ALL);
+ini_set('display_errors', true);
+
+// Define the path for the Joomla Platform.
+if (!defined('JPATH_PLATFORM'))
+{
+ $platform = getenv('JPLATFORM_HOME');
+ if ($platform)
+ {
+ define('JPATH_PLATFORM', realpath($platform));
+ }
+ else
+ {
+ define('JPATH_PLATFORM', realpath(__DIR__ . '/../../joomla/libraries'));
+ }
+}
+
+// Ensure that required path constants are defined.
+if (!defined('JPATH_BASE'))
+{
+ define('JPATH_BASE', realpath(__DIR__));
+}
+
+// Import the platform(s).
+require_once JPATH_PLATFORM . '/import.php';
+
+// Make sure that the Joomla Platform has been successfully loaded.
+if (!class_exists('JLoader'))
+{
+ exit('Joomla Platform not loaded.');
+}
+
+// Setup the autoloader for the Bach application classes.
+JLoader::registerPrefix('Bach', __DIR__);
46 code/model/default.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * @package Bach.Application
+ * @subpackage Model
+ *
+ * @copyright Copyright (C) {COPYRIGHT}. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+/**
+ * Default model for the Bach Application.
+ *
+ * @package Bach.Application
+ * @subpackage Model
+ * @since 1.0
+ */
+class BachModelDefault extends JModelBase
+{
+ /**
+ * Gets the time in either server, local (configuration) or universal time.
+ *
+ * @return JDate
+ *
+ * @since 1.0
+ */
+ public function getTime()
+ {
+ jimport('joomla.utilities.date');
+
+ // Get the timezone from the model state.
+ $timezone = $this->state->get('timezone');
+
+ if ($timezone)
+ {
+ // If the timezone is set, use it.
+ $time = new JDate(null, new DateTimeZone($timezone));
+ }
+ else
+ {
+ // If not, user the server timezone.
+ $time = new JDate(null, new DateTimeZone(@date_default_timezone_get()));
+ }
+
+ return $time;
+ }
+}
3  config/config.dist.json
@@ -0,0 +1,3 @@
+{
+ "timezone":"australia/perth"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.