-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
laravel.php
137 lines (102 loc) · 3.33 KB
/
laravel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
use Alfred\Workflows\Workflow;
use Algolia\AlgoliaSearch\SearchClient;
use Algolia\AlgoliaSearch\Support\UserAgent;
require __DIR__ . '/vendor/autoload.php';
$query = $argv[1];
preg_match('/^\h*?v?(master|(?:[\d]+)(?:\.[\d]+)?(?:\.[\dx]+)?)?\h*?(.*?)$/', $query, $matches);
if (! empty(trim($matches[1]))) {
$branch = $matches[1];
$query = trim($matches[2]);
} else {
$branch = getenv('branch');
}
if ($branch === 'latest') {
$branch = null;
} elseif (is_numeric($branch) && $branch > 5) {
// Format branch as docs URLs expect with .x suffix
$branch = "{$branch}.x";
}
$subtext = getenv('alfred_theme_subtext');
if (empty($subtext)) {
$subtext = '0';
}
$workflow = new Workflow;
$algolia = SearchClient::create('E3MIRNPJH5', '1fa3a8fec06eb1858d6ca137211225c0');
UserAgent::addCustomUserAgent('Alfred Workflow', '0.3.3');
$index = $algolia->initIndex('laravel');
$search = $index->search($query, ['facetFilters' => [
sprintf('version:%s', $branch ?: 'master'),
]]);
$results = $search['hits'];
$subtextSupported = $subtext === '0' || $subtext === '2';
if (empty($results)) {
$google = sprintf('https://www.google.com/search?q=%s', rawurlencode("laravel {$query}"));
$workflow->result()
->title($subtextSupported ? 'Search Google' : 'No match found. Search Google...')
->icon('google.png')
->subtitle(sprintf('No match found. Search Google for: "%s"', $query))
->arg($google)
->quicklookurl($google)
->valid(true);
$workflow->result()
->title($subtextSupported ? 'Open Docs' : 'No match found. Open docs...')
->icon('icon.png')
->subtitle('No match found. Open laravel.com/docs...')
->arg('https://laravel.com/docs/')
->quicklookurl('https://laravel.com/docs/')
->valid(true);
echo $workflow->output();
exit;
}
$urls = [];
foreach ($results as $hit) {
$url = $hit['url'];
if (in_array($url, $urls)) {
continue;
}
$urls[] = $url;
$hasText = isset($hit['_highlightResult']['content']['value']);
$title = $hit['hierarchy']['lvl0'];
$subtitle = subtitle($hit);
if (! $subtextSupported && $subtitle) {
$title = "{$title} » {$subtitle}";
}
$text = '';
if ($subtextSupported) {
$text = $subtitle;
if ($hasText) {
$text = $hit['_highlightResult']['content']['value'];
if ($subtitle) {
$title = "{$title} » {$subtitle}";
}
} else {
$text = sprintf('%s » %s', $hit['hierarchy']['lvl1'], $hit['hierarchy']['lvl2']);
}
}
$title = strip_tags(html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
$text = strip_tags(html_entity_decode($text, ENT_QUOTES, 'UTF-8'));
$text = preg_replace('/\s+/', ' ', $text);
$workflow->result()
->uid($hit['objectID'])
->title($title)
->autocomplete($title)
->subtitle($text)
->arg($url)
->quicklookurl($url)
->valid(true);
}
echo $workflow->output();
function subtitle($hit)
{
if (isset($hit['hierarchy']['lvl3'])) {
return $hit['hierarchy']['lvl3'];
}
if (isset($hit['hierarchy']['lvl2'])) {
return $hit['hierarchy']['lvl2'];
}
if (isset($hit['hierarchy']['lvl1'])) {
return $hit['hierarchy']['lvl1'];
}
return null;
}