Skip to content
This repository

Adds examples for three CLI applications #295

Merged
merged 3 commits into from over 2 years ago

3 participants

Andrew Eddie ianmacl Louis Landry
Andrew Eddie
Owner

Adds three CLI application examples:

  1. A simple hello world application.
  2. An application that shows you how command line arguments are accessed.
  3. An application that shows you how to use a configuration.php file.
ianmacl
Owner

Test Results

Total Tests: 1422
Assertions: 9913
Failures: 0
Errors: 0
Test Time: 178.692099

Louis Landry

Awesome.

Louis Landry LouisLandry merged commit d53c066 into from August 27, 2011
Louis Landry LouisLandry closed this August 27, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
60  examples/applications/101-hello-world/run.php
... ...
@@ -0,0 +1,60 @@
  1
+#!/usr/bin/php
  2
+<?php
  3
+/**
  4
+ * A "hello world" command line application built on the Joomla Platform.
  5
+ *
  6
+ * To run this example, adjust the executable path above to suite your operating system,
  7
+ * make this file executable and run the file.
  8
+ *
  9
+ * Alternatively, run the file using:
  10
+ *
  11
+ * php -f run.php
  12
+ *
  13
+ * @package    Joomla.Examples
  14
+ * @copyright  Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  15
+ * @license    GNU General Public License version 2 or later; see LICENSE
  16
+ */
  17
+
  18
+// We are a valid Joomla entry point.
  19
+// This is required to load the Joomla Platform import.php file.
  20
+define('_JEXEC', 1);
  21
+
  22
+// Setup the base path related constant.
  23
+// This is one of the few, mandatory constants needed for the Joomla Platform.
  24
+define('JPATH_BASE', dirname(__FILE__));
  25
+
  26
+// Bootstrap the application.
  27
+require dirname(dirname(dirname(dirname(__FILE__)))).'/libraries/import.php';
  28
+
  29
+// Import the JCli class from the platform.
  30
+jimport('joomla.application.cli');
  31
+
  32
+/**
  33
+ * A "hello world" command line application class.
  34
+ *
  35
+ * Simple command line applications extend the JCli class.
  36
+ *
  37
+ * @package  Joomla.Examples
  38
+ * @since    11.3
  39
+ */
  40
+class HelloWorld extends JCli
  41
+{
  42
+	/**
  43
+	 * Execute the application.
  44
+	 *
  45
+	 * The 'execute' method is the entry point for a command line application.
  46
+	 *
  47
+	 * @return  void
  48
+	 *
  49
+	 * @since   11.3
  50
+	 */
  51
+	public function execute()
  52
+	{
  53
+		// Send a string to standard output.
  54
+		$this->out('Hello world!');
  55
+	}
  56
+}
  57
+
  58
+// Instantiate the application object, passing the class name to JCli::getInstance
  59
+// and use chaining to execute the application.
  60
+JCli::getInstance('HelloWorld')->execute();
134  examples/applications/argv/run.php
... ...
@@ -0,0 +1,134 @@
  1
+#!/usr/bin/php
  2
+<?php
  3
+/**
  4
+ * An example command line application built on the Joomla Platform.
  5
+ *
  6
+ * To run this example, adjust the executable path above to suite your operating system,
  7
+ * make this file executable and run the file.
  8
+ *
  9
+ * @package    Joomla.Examples
  10
+ * @copyright  Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  11
+ * @license    GNU General Public License version 2 or later; see LICENSE
  12
+ */
  13
+
  14
+// We are a valid Joomla entry point.
  15
+define('_JEXEC', 1);
  16
+
  17
+// Setup the base path related constant.
  18
+define('JPATH_BASE', dirname(__FILE__));
  19
+
  20
+// Bootstrap the application.
  21
+require dirname(dirname(dirname(dirname(__FILE__)))).'/libraries/import.php';
  22
+
  23
+// Import the JCli class from the platform.
  24
+jimport('joomla.application.cli');
  25
+
  26
+/**
  27
+ * An example command line application class.
  28
+ *
  29
+ * This application shows how to access configuration file values.
  30
+ *
  31
+ * @package  Joomla.Examples
  32
+ * @since    11.3
  33
+ */
  34
+class Argv extends JCli
  35
+{
  36
+	/**
  37
+	 * Execute the application.
  38
+	 *
  39
+	 * @return  void
  40
+	 *
  41
+	 * @since   11.3
  42
+	 */
  43
+	public function execute()
  44
+	{
  45
+		// Print a blank line.
  46
+		$this->out();
  47
+		$this->out('JOOMLA PLATFORM ARGV EXAMPLE');
  48
+		$this->out('============================');
  49
+		$this->out();
  50
+
  51
+		// You can look for named command line arguments in the form of:
  52
+		// (a) -n value
  53
+		// (b) --name=value
  54
+		//
  55
+		// Try running file like this:
  56
+		// $ ./run.php -fa
  57
+		// $ ./run.php -f foo
  58
+		// $ ./run.php --set=match
  59
+		//
  60
+		// The values are accessed using the $this->input->get() method.
  61
+		// $this->input is an instance of a JInputCli object.
  62
+
  63
+		// This is an example of an option using short args (-).
  64
+		$value = $this->input->get('a');
  65
+		$this->out(
  66
+			sprintf(
  67
+				'%25s = %s', 'a',
  68
+				var_export($value, true)
  69
+			)
  70
+		);
  71
+
  72
+		$value = $this->input->get('f');
  73
+		$this->out(
  74
+			sprintf(
  75
+				'%25s = %s', 'f',
  76
+				var_export($value, true)
  77
+			)
  78
+		);
  79
+
  80
+		// This is an example of an option using long args (--).
  81
+		$value = $this->input->get('set');
  82
+		$this->out(
  83
+			sprintf(
  84
+				'%25s = %s', 'set',
  85
+				var_export($value, true)
  86
+			)
  87
+		);
  88
+
  89
+		// You can also apply defaults to the command line options.
  90
+		$value = $this->input->get('f', 'default');
  91
+		$this->out(
  92
+			sprintf(
  93
+				'%25s = %s', 'f (with default)',
  94
+				var_export($value, true)
  95
+			)
  96
+		);
  97
+
  98
+		// You can also apply input filters found in the JFilterInput class.
  99
+		// Try running this file like this:
  100
+		// $ ./run.php -f one2
  101
+
  102
+		$value = $this->input->get('f', 0, 'INT');
  103
+		$this->out(
  104
+			sprintf(
  105
+				'%25s = %s', 'f (cast to int)',
  106
+				var_export($value, true)
  107
+			)
  108
+		);
  109
+
  110
+		// Print out all the remaining command line arguments used to run this file.
  111
+		if (!empty($this->input->args))
  112
+		{
  113
+			$this->out();
  114
+			$this->out('These are the remaining arguments passed:');
  115
+			$this->out();
  116
+
  117
+			// Unallocated arguments are found in $this->input->args.
  118
+			// Try running the file like this:
  119
+			// $ ./run.php -f foo bar
  120
+
  121
+			foreach ($this->input->args as $arg)
  122
+			{
  123
+				$this->out($arg);
  124
+			}
  125
+		}
  126
+
  127
+		// Print a blank line at the end.
  128
+		$this->out();
  129
+	}
  130
+}
  131
+
  132
+// Instantiate the application object, passing the class name to JCli::getInstance
  133
+// and use chaining to execute the application.
  134
+JCli::getInstance('Argv')->execute();
48  examples/applications/show-config/configuration.php
... ...
@@ -0,0 +1,48 @@
  1
+<?php
  2
+/**
  3
+ * An example configuration file for an application built on the Joomla Platform.
  4
+ *
  5
+ * This file will be automatically loaded by the command line application.
  6
+ *
  7
+ * @package    Joomla.Examples
  8
+ * @copyright  Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  9
+ * @license    GNU General Public License version 2 or later; see LICENSE
  10
+ */
  11
+
  12
+// Prevent direct access to this file outside of a calling application.
  13
+defined('_JEXEC') or die;
  14
+
  15
+/**
  16
+ * An example configuration class for a Joomla Platform application.
  17
+ *
  18
+ * Declare each configuration value as a public property of this class.
  19
+ *
  20
+ * @package  Joomla.Examples
  21
+ * @since    11.3
  22
+ */
  23
+final class JConfig
  24
+{
  25
+	/**
  26
+	 * A configuration value.
  27
+	 *
  28
+	 * @var    integer
  29
+	 * @since  11.3
  30
+	 */
  31
+	public $weapons = 10;
  32
+
  33
+	/**
  34
+	 * A configuration value.
  35
+	 *
  36
+	 * @var    integer
  37
+	 * @since  11.3
  38
+	 */
  39
+	public $armour = 9;
  40
+
  41
+	/**
  42
+	 * A configuration value.
  43
+	 *
  44
+	 * @var    float
  45
+	 * @since  11.3
  46
+	 */
  47
+	public $health = 8.0;
  48
+}
125  examples/applications/show-config/run.php
... ...
@@ -0,0 +1,125 @@
  1
+#!/usr/bin/php
  2
+<?php
  3
+/**
  4
+ * An example command line application built on the Joomla Platform.
  5
+ *
  6
+ * To run this example, adjust the executable path above to suite your operating system,
  7
+ * make this file executable and run the file.
  8
+ *
  9
+ * Alternatively, run the file using:
  10
+ *
  11
+ * php -f run.php
  12
+ *
  13
+ * @package    Joomla.Examples
  14
+ * @copyright  Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  15
+ * @license    GNU General Public License version 2 or later; see LICENSE
  16
+ */
  17
+
  18
+// We are a valid Joomla entry point.
  19
+define('_JEXEC', 1);
  20
+
  21
+// Setup the base path related constant.
  22
+define('JPATH_BASE', dirname(__FILE__));
  23
+
  24
+// Bootstrap the application.
  25
+require dirname(dirname(dirname(dirname(__FILE__)))).'/libraries/import.php';
  26
+
  27
+// Import the JCli class from the platform.
  28
+jimport('joomla.application.cli');
  29
+
  30
+/**
  31
+ * An example command line application class.
  32
+ *
  33
+ * This application shows how to access configuration file values.
  34
+ *
  35
+ * @package  Joomla.Examples
  36
+ * @since    11.3
  37
+ */
  38
+class ShowConfig extends JCli
  39
+{
  40
+	/**
  41
+	 * Execute the application.
  42
+	 *
  43
+	 * @return  void
  44
+	 *
  45
+	 * @since   11.3
  46
+	 */
  47
+	public function execute()
  48
+	{
  49
+		// Print a blank line and new heading.
  50
+		$this->out();
  51
+		$this->out('Configuration settings loaded from configuration.php:');
  52
+
  53
+		// JCli will automatically look for and load 'configuration.php'.
  54
+		// Use the 'get' method to access any configuration properties.
  55
+		$this->out(
  56
+			sprintf(
  57
+				'%-25s = %2d', 'Default weapon strength',
  58
+				$this->get('weapons')
  59
+			)
  60
+		);
  61
+
  62
+		$this->out(
  63
+			sprintf(
  64
+				'%-25s = %2d', 'Default armour rating',
  65
+				$this->get('armour')
  66
+			)
  67
+		);
  68
+
  69
+		$this->out(
  70
+			sprintf(
  71
+				'%-25s = %4.1f', 'Default health level',
  72
+				$this->get('health')
  73
+			)
  74
+		);
  75
+
  76
+		// Print a blank line and new heading.
  77
+		$this->out();
  78
+		$this->out('System settings:');
  79
+
  80
+		// There are also a number of built in properties available, for example:
  81
+		$this->out(
  82
+			sprintf(
  83
+				'%-25s = %s', 'cwd',
  84
+				$this->get('cwd')
  85
+			)
  86
+		);
  87
+
  88
+		$this->out(
  89
+			sprintf(
  90
+				'%-25s = %s', 'execution.timestamp',
  91
+				$this->get('execution.timestamp')
  92
+			)
  93
+		);
  94
+
  95
+		$this->out(
  96
+			sprintf(
  97
+				'%-25s = %s', 'execution.timestamp',
  98
+				$this->get('execution.timestamp')
  99
+			)
  100
+		);
  101
+
  102
+		// Print a blank line and new heading.
  103
+		$this->out();
  104
+		$this->out('Custom settings:');
  105
+
  106
+		// We can also make custom settings during the execution of the the application using the 'set' method.
  107
+		$this->set('race', 'elf');
  108
+
  109
+		$this->out(
  110
+			sprintf(
  111
+				'%-25s = %s', 'Race',
  112
+				$this->get('race')
  113
+			)
  114
+		);
  115
+
  116
+		// Finish up.
  117
+		$this->out();
  118
+		$this->out('Thanks for playing!');
  119
+		$this->out();
  120
+	}
  121
+}
  122
+
  123
+// Instantiate the application object, passing the class name to JCli::getInstance
  124
+// and use chaining to execute the application.
  125
+JCli::getInstance('ShowConfig')->execute();
2  libraries/joomla/application/cli.php
@@ -27,7 +27,7 @@ class JCli
27 27
 	/**
28 28
 	 * The application input object.
29 29
 	 *
30  
-	 * @var    JInput
  30
+	 * @var    JInputCli
31 31
 	 * @since  11.1
32 32
 	 */
33 33
 	public $input;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.