Skip to content
Browse files

Initial upload.

  • Loading branch information...
1 parent 745cb14 commit cde502885f0a1a082016a1f4953519d0d6a8f38d @eddieajau committed Apr 30, 2012
Showing with 431 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +65 −3 README.md
  3. +43 −0 bin/run
  4. +189 −0 code/application/cli.php
  5. +51 −0 code/bootstrap.php
  6. +74 −0 code/model/plugins.php
  7. +8 −0 config/config.dist.json
View
1 .gitignore
@@ -0,0 +1 @@
+/.*
View
68 README.md
@@ -1,4 +1,66 @@
-jc-kodaly
-=========
+# Kodaly - A command line application to run a group of Joomla CMS plugins.
-A strategy for managing simple CRON plugins in the Joomla CMS via a standalone CLI application.
+Zoltán Kodály is known for his *Háry János Suite*, the second movement of which is entitled "Vienese Musical Clock". **Kodlay** is a very simple skeleton for a command line application that might be configured to run as a CRON task. It loads a folder of Joomla plugins. It demonstrates:
+
+* how to add database support in a command line application;
+* how to identify command line switches and variables; and
+* how to interact with a Joomla CMS.
+
+## 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 "Kodaly" 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 Kodaly application classes.
+JLoader::registerPrefix('Kodaly', __DIR__);</pre>
+
+You can search-and-replace the word "Kodaly" 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 into a folder called "joomla" under the same parent. For example:
+
+<pre>/parent
+../Kodaly &lt;-- this repository
+../joomla &lt;-- the Joomla platform</pre>
+
+The simplest way to do this is like this:
+
+<pre>mkdir Composers
+cd Composers
+git clone git://github.com/joomla/joomla-platform.git joomla
+git clone git://github.com/eddieajau/jc-Kodaly.git Kodaly</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.
+
+In addition, copy the ``config/config.dist.json`` file to ``config.json`` and adjust the database connection variables.
+
+## 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).
+
+By default, the plugins in the "cron" group will be executed if enabled, or another group can be specified using the "-g" or "--group" switch. The path to the head of the Joomla CMS must be included so that the application can find the plugins files.
+
+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.
View
43 bin/run
@@ -0,0 +1,43 @@
+#!/usr/bin/php
+<?php
+/**
+ * Command line script to run the Bach Application.
+ *
+ * @package Kodaly.Application
+ *
+ * @copyright Copyright (C) {COPYRIGHT}. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+// Bootstrap the application.
+$path = getenv('KODALY_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('KodalyApplicationCli');
+
+// 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());
+}
View
189 code/application/cli.php
@@ -0,0 +1,189 @@
+<?php
+/**
+ * @package Kodaly.Application
+ * @subpackage Application
+ *
+ * @copyright Copyright (C) {COPYRIGHT}. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+/**
+ * Kodaly CLI Application Class
+ *
+ * @package Kodaly.Application
+ * @subpackage Application
+ * @since 1.0
+ */
+class KodalyApplicationCli extends JApplicationCli
+{
+ /**
+ * A database driver for the application to use.
+ *
+ * @var JDatabaseDriver
+ * @since 1.0
+ */
+ protected $db;
+
+ /**
+ * 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;
+ }
+
+ // Initialise the database connection if not already.
+ if (empty($this->db))
+ {
+ $this->loadDatabase();
+ }
+
+ // Check command line switches.
+ $group = $this->input->get('g') || $this->input->get('group') || 'cron';
+
+ if (empty($this->input->args))
+ {
+ $this->_help();
+
+ return;
+ }
+
+ $cmsPath = $this->input->args[0];
+
+ // Create a registry for the model state.
+ $state = new JRegistry;
+
+ $model = new KodalyModelPlugins($state, $this->db);
+
+ // Load the plugins.
+ $plugins = $model->load($cmsPath, $group);
+
+ // Run the plugins.
+ foreach ($plugins as $plugin)
+ {
+ JLog::add(sprintf('Running "%s"', $plugin->getName()));
+
+ $plugin->run();
+ }
+ }
+
+ /**
+ * Allows the application to load a custom or default database driver.
+ *
+ * @param JDatabaseDriver $driver An optional database driver object. If omitted, the application driver is created.
+ *
+ * @return JApplicationBase This method is chainable.
+ *
+ * @since 12.1
+ */
+ public function loadDatabase(JDatabaseDriver $driver = null)
+ {
+ if ($driver === null)
+ {
+ $this->db = JDatabaseDriver::getInstance(
+ array(
+ 'driver' => $this->get('db_driver'),
+ 'host' => $this->get('db_host'),
+ 'user' => $this->get('db_user'),
+ 'password' => $this->get('db_pass'),
+ 'database' => $this->get('db_name'),
+ 'prefix' => $this->get('db_prefix')
+ )
+ );
+
+ // Select the database.
+ $this->db->select($this->get('db_name'));
+ }
+ // Use the given database driver object.
+ else
+ {
+ $this->db = $driver;
+ }
+
+ // Set the database to our static cache.
+ JFactory::$database = $this->db;
+
+ return $this;
+ }
+
+ /**
+ * 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('Kodaly_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('Kodaly 1.0.');
+ $this->out();
+ $this->out('An application to run a group of Joomla CMS plugins.');
+ $this->out();
+ $this->out('Usage: run [switches] path-to-Joomla-CMS');
+ $this->out();
+ $this->out(' -h | --help Prints this usage information.');
+ $this->out(' -g | --group The plugin group to trigger (default is "cron").');
+ $this->out();
+ $this->out('Examples: ./run -h');
+ $this->out('Examples: ./run -g cron7 /var/www/joomla');
+ $this->out();
+ }
+}
View
51 code/bootstrap.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * Bootstrap file for the Kodaly Application.
+ *
+ * @package Kodaly.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 Kodaly application classes.
+JLoader::registerPrefix('Kodaly', __DIR__);
View
74 code/model/plugins.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * @package Kodaly.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 Kodaly Application.
+ *
+ * @package Kodaly.Application
+ * @subpackage Model
+ * @since 1.0
+ */
+class KodalyModelDefault extends JModelDatabase
+{
+ /**
+ * A dummy method to make this look like a dispatcher.
+ *
+ * @return void
+ */
+ public function attach()
+ {
+ // Do nothing.
+ }
+
+ /**
+ * Loads the plugins from the CMS location.
+ *
+ * @return void
+ */
+ public function load($cmsPath, $group = 'cron')
+ {
+ // Initialiase variables.
+ jimport('joomla.filesystem.path');
+ jimport('joomla.html.parameter');
+ jimport('joomla.plugin.plugin');
+
+ // Must do a lot of manual work. Can't afford to trip the session (mainly a call to JFactory::getUser()).
+ $query = $this->db->getQuery(true);
+
+ // Get a list of the plugins from the database.
+ $query->select('p.*')
+ ->from('#__extensions AS p')
+ ->where('p.enabled = 1')
+ ->where('p.type = ' . $db->quote('plugin'))
+ ->where('p.folder = ' . $db->quote($group))
+ ->order('p.ordering');
+ $this->db->setQuery($query);
+
+ $plugins = $this->db->loadObjectList();
+
+ $result = array();
+
+ // Convert the parameters for each plugin.
+ foreach ($plugins as $plugin)
+ {
+ $className = 'plg' . ucfirst($plugin->folder) . ucfirst($plugin->element);
+
+ // Preload the plugins.
+ $path = JPath::clean(sprintf('%s/plugins/%s/%s/%s.php', $cmsPath, $plugin->folder, $plugin->element, $plugin->element));
+ require_once $path;
+
+ if (class_exists($className))
+ {
+ $result[] = new $className($this, array('params' => new JRegistry($plugin->params)));
+ }
+ }
+
+ return $result;
+ }
+}
View
8 config/config.dist.json
@@ -0,0 +1,8 @@
+{
+ "db_driver":"mysqli",
+ "db_host":"127.0.0.1",
+ "db_user":"root",
+ "db_pass":"",
+ "db_name":"joomla-cms-database-name",
+ "db_prefix":"joomla-cms-table-prefix",
+}

0 comments on commit cde5028

Please sign in to comment.
Something went wrong with that request. Please try again.