Doctrine 1
  1. Doctrine 1
  2. DC-115

Doctrine_Cli and Doctrine_Task: further improvements to creation and registration of custom tasks

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.2.0-ALPHA2
    • Fix Version/s: 1.2.0-BETA1
    • Component/s: Cli
    • Labels:
      None
    • Environment:
      Darwin, PHP 5.3, MacBook

      Description

      Having worked with - and experienced the 'inconveniences' of - my previous changes (see r6475), I have made further improvements to Doctrine_Cli, and a small change to Doctrine_Task, and have achieved the following.

      • Creating and working with custom tasks is now even easier.
      • Doctrine_Cli is even more powerful since it is now that much easier to extend.
      • Doctrine_Cli is more testable - though there's still room for improvement.

      The enclosed patch was created against 1.2 r6535. I have merged changes committed since r6475 - Jon's changes in r6484.

      Further details:

      Doctrine tasks are always included and 'registered' on startup. By default, loaded custom tasks are then registered.

      Custom tasks need only extend Doctrine_Task - there are no special naming conventions to follow.

      Automatic loading of custom tasks can be disabled by setting the new "autoregister_custom_tasks" config option to FALSE (TRUE is the default).

      The following code includes and registers Doctrine core Tasks, and then registers loaded custom tasks.

      $cli = new Doctrine_Cli();
      $cli->run($_SERVER['argv']);

      The following code includes and registers only Doctrine core Tasks.

      $oCli = new Doctrine_Cli(array('autoregister_custom_tasks' => false));
      $oCli->run($_SERVER['argv']);

      The following code includes and registers Doctrine core Tasks, and then registers a custom task loaded after the CLI was instantiated.

      $oCli = new Doctrine_Cli();

      require_once(dirname(_FILE_) . '/TestTask02.php');

      //Either...:
      $oCli->registerTaskClass('Doctrine_Cli_TestCase_TestTask02');
      //...Or:
      $oCli->registerIncludedTaskClasses();

      $oCli->run($_SERVER['argv']);

      (These code examples can be found in /tests/CliTestCase.)

      Responsibility for naming tasks lies with the tasks themselves. Tasks will still receive a name derived from the fully-qualified (i.e. including namespaces) name of their class, but it is now possible to specify the name at design time - you might want to do this if the default task name would be very long or undesirable for some reason.

      For example:

      namespace fully\qualified;

      class CustomTaskName extends \Doctrine_Task {

      //This task would otherwise be registered as "fully-qualified-custom-task-name"
      public $taskName = 'custom-task-name';

      public function execute () {}
      }

      A CLI can now be instructed to rethrow exceptions. This is very useful if you're running a batch of tasks in a single script and you need processing to stop if one of them fails.

      Simply set the new "rethrow_exceptions" config option to TRUE to enable this behaviour - the default is FALSE.

      For example:

      $oCli = new Doctrine_Cli(array(
      'autoregister_custom_tasks' => false,
      'rethrow_exceptions' => true,
      ));

      $oCli->run($_SERVER['argv']);

      I think there are still one or two things I'd like to look at, but I wanted to get the bulk of the work in so I don't hold you up.

      Oh, and I'd like to apologise for having moved everything around. I wouldn't normally, but in trying to make sense of things, I found it necessary.

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Dan Bettles
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: