Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / drush / drush / examples / Commands / XkcdCommands.php
1 <?php
2 namespace Drush\Commands;
3
4 use Drush\Exec\ExecTrait;
5
6 /**
7  * Run these commands using the --include option - e.g. `drush --include=/path/to/drush/examples xkcd`
8  */
9
10 class XkcdCommands extends DrushCommands
11 {
12
13     use ExecTrait;
14
15   /**
16    * Retrieve and display xkcd cartoons.
17    *
18    * @command xkcd:fetch
19    * @param $search Optional argument to retrieve the cartoons matching an index number, keyword search or "random". If omitted the latest cartoon will be retrieved.
20    * @option image-viewer Command to use to view images (e.g. xv, firefox). Defaults to "display" (from ImageMagick).
21    * @option google-custom-search-api-key Google Custom Search API Key, available from https://code.google.com/apis/console/. Default key limited to 100 queries/day globally.
22    * @usage drush xkcd
23    *   Retrieve and display the latest cartoon.
24    * @usage drush xkcd sandwich
25    *   Retrieve and display cartoons about sandwiches.
26    * @usage drush xkcd 123 --image-viewer=eog
27    *   Retrieve and display cartoon #123 in eog.
28    * @usage drush xkcd random --image-viewer=firefox
29    *   Retrieve and display a random cartoon in Firefox.
30    * @aliases xkcd
31    */
32     public function fetch($search = null, $options = ['image-viewer' => 'open', 'google-custom-search-api-key' => 'AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek'])
33     {
34         if (empty($search)) {
35             $this->startBrowser('http://xkcd.com');
36         } elseif (is_numeric($search)) {
37             $this->startBrowser('http://xkcd.com/' . $search);
38         } elseif ($search == 'random') {
39             $xkcd_response = @json_decode(file_get_contents('http://xkcd.com/info.0.json'));
40             if (!empty($xkcd_response->num)) {
41                 $this->startBrowser('http://xkcd.com/' . rand(1, $xkcd_response->num));
42             }
43         } else {
44             // This uses an API key with a limited number of searches per.
45             $search_response = @json_decode(file_get_contents('https://www.googleapis.com/customsearch/v1?key=' . $options['google-custom-search-api-key'] . '&cx=012652707207066138651:zudjtuwe28q&q=' . $search));
46             if (!empty($search_response->items)) {
47                 foreach ($search_response->items as $item) {
48                     $this->startBrowser($item->link);
49                 }
50             } else {
51                 throw new \Exception(dt('The search failed or produced no results.'));
52             }
53         }
54     }
55 }