Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Commands / core / NotifyCommands.php
1 <?php
2 namespace Drush\Commands\core;
3
4 use Consolidation\AnnotatedCommand\CommandData;
5 use Drush\Commands\DrushCommands;
6 use Drush\Drush;
7 use Symfony\Component\Config\Definition\Exception\Exception;
8 use Webmozart\PathUtil\Path;
9
10 class NotifyCommands extends DrushCommands
11 {
12     /**
13      * @hook option *
14      * @option notify Notify upon command completion. If set to a number, commands that finish in fewer seconds won't notify.
15      */
16     public function optionsetNotify()
17     {
18     }
19
20     /**
21      * @hook pre-command *
22      */
23     public function registerShutdown(CommandData $commandData)
24     {
25         register_shutdown_function([$this, 'shutdown'], $commandData);
26     }
27
28     public function shutdown(CommandData $commandData)
29     {
30
31         $annotationData = $commandData->annotationData();
32         if (!$cmd = $annotationData['command']) {
33             return;
34         }
35
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);
40             }
41         }
42     }
43
44     /**
45      * Prepares and dispatches notifications to delivery mechanisms.
46      *
47      * You may avoid routing a message to secondary messaging mechanisms (e.g. audio),
48      * by direct use of the delivery functions.
49      *
50      * @param string $msg
51      *   Message to send via notification.
52      */
53     public static function shutdownSend($msg, CommandData $commandData)
54     {
55         self::shutdownSendText($msg, $commandData);
56     }
57
58     /**
59      * Send text-based system notification.
60      *
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.
63      *
64      * @param string $msg
65      *   Message text for delivery.
66      *
67      * @return bool
68      *   TRUE on success, FALSE on failure
69      */
70     public static function shutdownSendText($msg, CommandData $commandData)
71     {
72         $override = Drush::config()->get('notify.cmd');
73
74         if (!empty($override)) {
75             $cmd = $override;
76         } else {
77             switch (PHP_OS) {
78                 case 'Darwin':
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.');
81                     break;
82                 case 'Linux':
83                 default:
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.');
87                     break;
88             }
89         }
90
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'));
93         }
94
95         return true;
96     }
97
98     /**
99      * Identify if the given Drush request should trigger a notification.
100      *
101      * @param $command
102      *   Name of the command.
103      *
104      * @return
105      *   Boolean
106      */
107     public static function isAllowed(CommandData $commandData)
108     {
109         $duration = Drush::config()->get('notify.duration');
110         $execution = time() - $_SERVER['REQUEST_TIME'];
111
112         return ($duration === true ||
113         (is_numeric($duration) && $duration > 0 && $execution > $duration));
114     }
115 }