2 namespace Drush\Commands\core;
4 use Consolidation\AnnotatedCommand\CommandData;
5 use Drush\Commands\DrushCommands;
7 use Symfony\Component\Config\Definition\Exception\Exception;
8 use Webmozart\PathUtil\Path;
10 class NotifyCommands extends DrushCommands
14 * @option notify Notify upon command completion. If set to a number, commands that finish in fewer seconds won't notify.
16 public function optionsetNotify()
23 public function registerShutdown(CommandData $commandData)
25 register_shutdown_function([$this, 'shutdown'], $commandData);
28 public function shutdown(CommandData $commandData)
31 $annotationData = $commandData->annotationData();
32 if (!$cmd = $annotationData['command']) {
36 if (Drush::config()->get('notify.duration')) {
37 if (self::isAllowed($commandData)) {
38 $msg = dt("Command '!command' completed.", ['!command' => $cmd]);
39 self::shutdownSend($msg, $commandData);
45 * Prepares and dispatches notifications to delivery mechanisms.
47 * You may avoid routing a message to secondary messaging mechanisms (e.g. audio),
48 * by direct use of the delivery functions.
51 * Message to send via notification.
53 public static function shutdownSend($msg, CommandData $commandData)
55 self::shutdownSendText($msg, $commandData);
59 * Send text-based system notification.
61 * This is the automatic, default behavior. It is intended for use with tools
62 * such as libnotify in Linux and Notification Center on OSX.
65 * Message text for delivery.
68 * TRUE on success, FALSE on failure
70 public static function shutdownSendText($msg, CommandData $commandData)
72 $override = Drush::config()->get('notify.cmd');
74 if (!empty($override)) {
79 $cmd = 'terminal-notifier -message %s -title Drush';
80 $error_message = dt('terminal-notifier command failed. Please install it from https://github.com/alloy/terminal-notifier.');
84 $icon = Path::join(DRUSH_BASE_PATH, 'drush_logo-black.png');
85 $cmd = "notify-send %s -i $icon";
86 $error_message = dt('notify-send command failed. Please install it as per http://coderstalk.blogspot.com/2010/02/how-to-install-notify-send-in-ubuntu.html.');
91 if (!drush_shell_exec($cmd, $msg)) {
92 throw new \Exception($error_message . ' ' . dt('Or you may specify an alternate command to run by setting notify:cmd in a drush.yml file'));
99 * Identify if the given Drush request should trigger a notification.
102 * Name of the command.
107 public static function isAllowed(CommandData $commandData)
109 $duration = Drush::config()->get('notify.duration');
110 $execution = time() - $_SERVER['REQUEST_TIME'];
112 return ($duration === true ||
113 (is_numeric($duration) && $duration > 0 && $execution > $duration));