Skip to content

Commit d5e2794

Browse files
committed
Upgrade deps to support php 8.0 and refactor php code
1 parent 821a3cd commit d5e2794

File tree

5 files changed

+102
-140
lines changed

5 files changed

+102
-140
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"license": "MIT",
1212
"require": {
1313
"joetannenbaum/alfred-workflow": "^0.1.0",
14-
"algolia/algoliasearch-client-php": "^1.27",
15-
"erusev/parsedown": "^1.7"
14+
"algolia/algoliasearch-client-php": "^2.7"
1615
}
1716
}

functions.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
function getResults($algolia, $indexName, $query)
4+
{
5+
$index = $algolia->initIndex($indexName);
6+
7+
return $index->search($query)['hits'];
8+
}
9+
10+
function getTitle($hit)
11+
{
12+
if (isset($hit['hierarchy']['lvl6'])) {
13+
return [$hit['hierarchy']['lvl6'], 6];
14+
}
15+
16+
if (isset($hit['hierarchy']['lvl5'])) {
17+
return [$hit['hierarchy']['lvl5'], 5];
18+
}
19+
20+
if (isset($hit['hierarchy']['lvl4'])) {
21+
return [$hit['hierarchy']['lvl4'], 4];
22+
}
23+
24+
if (isset($hit['hierarchy']['lvl3'])) {
25+
return [$hit['hierarchy']['lvl3'], 3];
26+
}
27+
28+
if (isset($hit['hierarchy']['lvl2'])) {
29+
return [$hit['hierarchy']['lvl2'], 2];
30+
}
31+
32+
if (isset($hit['hierarchy']['lvl1'])) {
33+
return [$hit['hierarchy']['lvl1'], 1];
34+
}
35+
36+
return null;
37+
}
38+
39+
function getSubtitle($hit, $titleLevel)
40+
{
41+
$currentLevel = 0;
42+
$subtitle = $hit['hierarchy']['lvl0'];
43+
44+
while ($currentLevel < $titleLevel) {
45+
$currentLevel++;
46+
$subtitle .= ' » ' . $hit['hierarchy']['lvl' . $currentLevel];
47+
}
48+
49+
return $subtitle;
50+
}

tailwindcss.php

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,43 @@
22

33
use Alfred\Workflows\Workflow;
44

5-
use AlgoliaSearch\Client as Algolia;
6-
use AlgoliaSearch\Version as AlgoliaUserAgent;
5+
use Algolia\AlgoliaSearch\SearchClient as Algolia;
6+
use Algolia\AlgoliaSearch\Support\UserAgent as AlgoliaUserAgent;
77

88
require __DIR__ . '/vendor/autoload.php';
9+
require __DIR__ . '/functions.php';
910

1011
$query = $argv[1];
11-
//$branch = empty($_ENV['branch']) ? 'master' : $_ENV['branch'];
12-
//$subtext = empty($_ENV['alfred_theme_subtext']) ? '0' : $_ENV['alfred_theme_subtext'];
1312

1413
$workflow = new Workflow;
15-
$parsedown = new Parsedown;
16-
$algolia = new Algolia('R90K1756AM', 'a6e52654d6b591febdf42b07e0e7374a');
14+
$algolia = Algolia::create('R90K1756AM', 'a6e52654d6b591febdf42b07e0e7374a');
1715

18-
AlgoliaUserAgent::addSuffixUserAgentSegment('TailwindCSS Alfred Workflow', '2.0.0');
16+
AlgoliaUserAgent::addCustomUserAgent('TailwindCSS Alfred Workflow', '2.0.1');
1917

20-
$index = $algolia->initIndex('v2_tailwindcss');
21-
$search = $index->search($query);
22-
$results = $search['hits'];
23-
24-
$subtextSupported = $subtext === '0' || $subtext === '2';
18+
$results = getResults($algolia, 'v2_tailwindcss', $query);
2519

2620
if (empty($results)) {
27-
if (empty($results)) {
28-
$workflow->result()
29-
->title('No matches')
30-
->icon('google.png')
31-
->subtitle("No match found in the docs. Search Google for: \"TailwindCSS+{$query}\"")
32-
->arg("https://www.google.com/search?q=tailwindcss+{$query}")
33-
->quicklookurl("https://www.google.com/search?q=tailwindcss+{$query}")
34-
->valid(true);
21+
$workflow->result()
22+
->title('No matches')
23+
->icon('google.png')
24+
->subtitle("No match found in the docs. Search Google for: \"TailwindCSS+{$query}\"")
25+
->arg("https://www.google.com/search?q=tailwindcss+{$query}")
26+
->quicklookurl("https://www.google.com/search?q=tailwindcss+{$query}")
27+
->valid(true);
3528

36-
echo $workflow->output();
37-
exit;
38-
}
29+
echo $workflow->output();
3930
exit;
4031
}
4132

42-
$urls = [];
43-
44-
4533
foreach ($results as $hit) {
46-
$highestLvl = $hit['hierarchy']['lvl6'] ? 6 : (
47-
$hit['hierarchy']['lvl5'] ? 5 : (
48-
$hit['hierarchy']['lvl4'] ? 4 : (
49-
$hit['hierarchy']['lvl3'] ? 3 : (
50-
$hit['hierarchy']['lvl2'] ? 2 : (
51-
$hit['hierarchy']['lvl1'] ? 1 : 0
52-
)
53-
)
54-
)
55-
)
56-
);
57-
58-
$title = $hit['hierarchy']['lvl' . $highestLvl];
59-
$currentLvl = 0;
60-
$subtitle = $hit['hierarchy']['lvl0'];
61-
while ($currentLvl < $highestLvl) {
62-
$currentLvl = $currentLvl + 1;
63-
$subtitle = $subtitle . ' » ' . $hit['hierarchy']['lvl' . $currentLvl];
64-
}
34+
list($title, $titleLevel) = getTitle($hit);
35+
$title = html_entity_decode($title);
6536

6637
$workflow->result()
6738
->uid($hit['objectID'])
6839
->title($title)
6940
->autocomplete($title)
70-
->subtitle($subtitle)
41+
->subtitle(html_entity_decode(getSubtitle($hit, $titleLevel)))
7142
->arg($hit['url'])
7243
->quicklookurl($hit['url'])
7344
->valid(true);

v0_tailwindcss.php

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,43 @@
22

33
use Alfred\Workflows\Workflow;
44

5-
use AlgoliaSearch\Client as Algolia;
6-
use AlgoliaSearch\Version as AlgoliaUserAgent;
5+
use Algolia\AlgoliaSearch\SearchClient as Algolia;
6+
use Algolia\AlgoliaSearch\Support\UserAgent as AlgoliaUserAgent;
77

88
require __DIR__ . '/vendor/autoload.php';
9+
require __DIR__ . '/functions.php';
910

1011
$query = $argv[1];
11-
//$branch = empty($_ENV['branch']) ? 'master' : $_ENV['branch'];
12-
//$subtext = empty($_ENV['alfred_theme_subtext']) ? '0' : $_ENV['alfred_theme_subtext'];
1312

1413
$workflow = new Workflow;
15-
$parsedown = new Parsedown;
16-
$algolia = new Algolia('R90K1756AM', 'a6e52654d6b591febdf42b07e0e7374a');
14+
$algolia = Algolia::create('R90K1756AM', 'a6e52654d6b591febdf42b07e0e7374a');
1715

18-
AlgoliaUserAgent::addSuffixUserAgentSegment('TailwindCSS Alfred Workflow', '2.0.0');
16+
AlgoliaUserAgent::addCustomUserAgent('TailwindCSS Alfred Workflow', '2.0.1');
1917

20-
$index = $algolia->initIndex('v0_tailwindcss');
21-
$search = $index->search($query);
22-
$results = $search['hits'];
23-
24-
$subtextSupported = $subtext === '0' || $subtext === '2';
18+
$results = getResults($algolia, 'v0_tailwindcss', $query);
2519

2620
if (empty($results)) {
27-
if (empty($results)) {
28-
$workflow->result()
29-
->title('No matches')
30-
->icon('google.png')
31-
->subtitle("No match found in the docs. Search Google for: \"TailwindCSS+{$query}\"")
32-
->arg("https://www.google.com/search?q=tailwindcss+{$query}")
33-
->quicklookurl("https://www.google.com/search?q=tailwindcss+{$query}")
34-
->valid(true);
21+
$workflow->result()
22+
->title('No matches')
23+
->icon('google.png')
24+
->subtitle("No match found in the docs. Search Google for: \"TailwindCSS+{$query}\"")
25+
->arg("https://www.google.com/search?q=tailwindcss+{$query}")
26+
->quicklookurl("https://www.google.com/search?q=tailwindcss+{$query}")
27+
->valid(true);
3528

36-
echo $workflow->output();
37-
exit;
38-
}
29+
echo $workflow->output();
3930
exit;
4031
}
4132

42-
$urls = [];
43-
44-
4533
foreach ($results as $hit) {
46-
$highestLvl = $hit['hierarchy']['lvl6'] ? 6 : (
47-
$hit['hierarchy']['lvl5'] ? 5 : (
48-
$hit['hierarchy']['lvl4'] ? 4 : (
49-
$hit['hierarchy']['lvl3'] ? 3 : (
50-
$hit['hierarchy']['lvl2'] ? 2 : (
51-
$hit['hierarchy']['lvl1'] ? 1 : 0
52-
)
53-
)
54-
)
55-
)
56-
);
57-
58-
$title = $hit['hierarchy']['lvl' . $highestLvl];
59-
$currentLvl = 0;
60-
$subtitle = $hit['hierarchy']['lvl0'];
61-
while ($currentLvl < $highestLvl) {
62-
$currentLvl = $currentLvl + 1;
63-
$subtitle = $subtitle . ' » ' . $hit['hierarchy']['lvl' . $currentLvl];
64-
}
34+
list($title, $titleLevel) = getTitle($hit);
35+
$title = html_entity_decode($title);
6536

6637
$workflow->result()
6738
->uid($hit['objectID'])
6839
->title($title)
6940
->autocomplete($title)
70-
->subtitle($subtitle)
41+
->subtitle(html_entity_decode(getSubtitle($hit, $titleLevel)))
7142
->arg($hit['url'])
7243
->quicklookurl($hit['url'])
7344
->valid(true);

v1_tailwindcss.php

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,43 @@
22

33
use Alfred\Workflows\Workflow;
44

5-
use AlgoliaSearch\Client as Algolia;
6-
use AlgoliaSearch\Version as AlgoliaUserAgent;
5+
use Algolia\AlgoliaSearch\SearchClient as Algolia;
6+
use Algolia\AlgoliaSearch\Support\UserAgent as AlgoliaUserAgent;
77

88
require __DIR__ . '/vendor/autoload.php';
9+
require __DIR__ . '/functions.php';
910

1011
$query = $argv[1];
11-
//$branch = empty($_ENV['branch']) ? 'master' : $_ENV['branch'];
12-
//$subtext = empty($_ENV['alfred_theme_subtext']) ? '0' : $_ENV['alfred_theme_subtext'];
1312

1413
$workflow = new Workflow;
15-
$parsedown = new Parsedown;
16-
$algolia = new Algolia('R90K1756AM', 'a6e52654d6b591febdf42b07e0e7374a');
14+
$algolia = Algolia::create('R90K1756AM', 'a6e52654d6b591febdf42b07e0e7374a');
1715

18-
AlgoliaUserAgent::addSuffixUserAgentSegment('TailwindCSS Alfred Workflow', '2.0.0');
16+
AlgoliaUserAgent::addCustomUserAgent('TailwindCSS Alfred Workflow', '2.0.1');
1917

20-
$index = $algolia->initIndex('tailwindcss');
21-
$search = $index->search($query);
22-
$results = $search['hits'];
23-
24-
$subtextSupported = $subtext === '0' || $subtext === '2';
18+
$results = getResults($algolia, 'tailwindcss', $query);
2519

2620
if (empty($results)) {
27-
if (empty($results)) {
28-
$workflow->result()
29-
->title('No matches')
30-
->icon('google.png')
31-
->subtitle("No match found in the docs. Search Google for: \"TailwindCSS+{$query}\"")
32-
->arg("https://www.google.com/search?q=tailwindcss+{$query}")
33-
->quicklookurl("https://www.google.com/search?q=tailwindcss+{$query}")
34-
->valid(true);
21+
$workflow->result()
22+
->title('No matches')
23+
->icon('google.png')
24+
->subtitle("No match found in the docs. Search Google for: \"TailwindCSS+{$query}\"")
25+
->arg("https://www.google.com/search?q=tailwindcss+{$query}")
26+
->quicklookurl("https://www.google.com/search?q=tailwindcss+{$query}")
27+
->valid(true);
3528

36-
echo $workflow->output();
37-
exit;
38-
}
29+
echo $workflow->output();
3930
exit;
4031
}
4132

42-
$urls = [];
43-
44-
4533
foreach ($results as $hit) {
46-
$highestLvl = $hit['hierarchy']['lvl6'] ? 6 : (
47-
$hit['hierarchy']['lvl5'] ? 5 : (
48-
$hit['hierarchy']['lvl4'] ? 4 : (
49-
$hit['hierarchy']['lvl3'] ? 3 : (
50-
$hit['hierarchy']['lvl2'] ? 2 : (
51-
$hit['hierarchy']['lvl1'] ? 1 : 0
52-
)
53-
)
54-
)
55-
)
56-
);
57-
58-
$title = $hit['hierarchy']['lvl' . $highestLvl];
59-
$currentLvl = 0;
60-
$subtitle = $hit['hierarchy']['lvl0'];
61-
while ($currentLvl < $highestLvl) {
62-
$currentLvl = $currentLvl + 1;
63-
$subtitle = $subtitle . ' » ' . $hit['hierarchy']['lvl' . $currentLvl];
64-
}
34+
list($title, $titleLevel) = getTitle($hit);
35+
$title = html_entity_decode($title);
6536

6637
$workflow->result()
6738
->uid($hit['objectID'])
6839
->title($title)
6940
->autocomplete($title)
70-
->subtitle($subtitle)
41+
->subtitle(html_entity_decode(getSubtitle($hit, $titleLevel)))
7142
->arg($hit['url'])
7243
->quicklookurl($hit['url'])
7344
->valid(true);

0 commit comments

Comments
 (0)