Skip to content

Commit 9ed6045

Browse files
committed
Update command line output content
1 parent 6315245 commit 9ed6045

File tree

3 files changed

+83
-65
lines changed

3 files changed

+83
-65
lines changed

bin/update-psl

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,6 @@ $reset = chr(27)."[0m";
5050
$redbg = chr(27)."[41m";
5151
$yellow = chr(27)."[33m";
5252

53-
$script = basename(__FILE__);
54-
$helpText = <<<HELP
55-
$yellow
56-
Pdp\Installer v1.0 $reset
57-
58-
The following script updates your local cache to ease library usage.
59-
=====
60-
61-
Usage: $yellow$script [OPTIONS]$reset
62-
63-
$green--%s$reset refreshes the Public Suffix List cache
64-
$green--%s$reset set the URL to use to refresh the PSL cache ({$yellow}default:$reset %s)
65-
$green--%s$reset refreshes the IANA Root Zone Database cache
66-
$green--%s$reset set the URL to use to refresh the RZD cache ({$yellow}default:$reset %s)
67-
$green--%s$reset set the TTL for the cache ({$yellow}default:$reset '1 DAY')
68-
$green--%s$reset set the cache root directory
69-
$green-h, --help$reset show the following help message
70-
71-
Examples:
72-
73-
Refresh all caches using the default settings
74-
$yellow$script$reset
75-
76-
Refresh the PSL cache for a TTL of 3 DAY
77-
$yellow$script --psl --ttl="3 DAYS"$reset
78-
79-
Refresh the cache located in another cache directory
80-
$yellow$script --cache-dir=/temp$reset
81-
HELP;
82-
83-
$helpText = sprintf($helpText,
84-
Installer::REFRESH_PSL_KEY,
85-
Installer::REFRESH_PSL_URL_KEY,
86-
Manager::PSL_URL,
87-
Installer::REFRESH_RZD_KEY,
88-
Installer::REFRESH_RZD_URL_KEY,
89-
Manager::RZD_URL,
90-
Installer::TTL_KEY,
91-
Installer::CACHE_DIR_KEY
92-
);
93-
9453
$arguments = array_replace([
9554
Installer::CACHE_DIR_KEY => '',
9655
Installer::REFRESH_PSL_KEY => false,
@@ -108,6 +67,50 @@ $arguments = array_replace([
10867
]));
10968

11069
if (isset($arguments['help']) || isset($arguments['h'])) {
70+
$default_cache_dir = dirname(__DIR__).'/data';
71+
$script = basename(__FILE__);
72+
$helpText = <<<HELP
73+
{$yellow}Usage:$reset
74+
$script [options]
75+
76+
{$yellow}Options:$reset
77+
$green --%s$reset refreshes the Public Suffix List cache
78+
$green --%s=URL$reset set the URL to use to refresh the PSL cache ({$yellow}default:$reset %s)
79+
$green --%s$reset refreshes the IANA Root Zone Database cache
80+
$green --%s=URL$reset set the URL to use to refresh the RZD cache ({$yellow}default:$reset %s)
81+
$green --%s=TTL$reset set the TTL for the cache ({$yellow}default:$reset '1 DAY')
82+
$green --%s=CACHE-DIR$reset set the cache root directory ({$yellow}default:$reset $default_cache_dir')
83+
$green-h, --help$reset show the following help message
84+
85+
{$yellow}Help:$reset
86+
The {$green}update-psl$reset command updates your PDP local cache.
87+
88+
{$yellow}Examples:$reset
89+
90+
Refresh all caches using the default settings
91+
$green$script$reset
92+
93+
Refresh only the PSL cache for a TTL of 3 DAY
94+
$green$script --psl --ttl="3 DAYS"$reset
95+
96+
Refresh all caches using another cache directory
97+
$green$script --cache-dir=/temp$reset
98+
99+
Read more at https://github.com/jeremykendall/php-domain-parser/
100+
101+
HELP;
102+
103+
$helpText = sprintf($helpText,
104+
Installer::REFRESH_PSL_KEY,
105+
Installer::REFRESH_PSL_URL_KEY,
106+
Manager::PSL_URL,
107+
Installer::REFRESH_RZD_KEY,
108+
Installer::REFRESH_RZD_URL_KEY,
109+
Manager::RZD_URL,
110+
Installer::TTL_KEY,
111+
Installer::CACHE_DIR_KEY
112+
);
113+
111114
success($helpText);
112115

113116
die(0);
@@ -120,6 +123,11 @@ if (!extension_loaded('curl')) {
120123
}
121124

122125
$installer = Installer::createFromCacheDir(new Logger( STDOUT, STDERR), $arguments[Installer::CACHE_DIR_KEY]);
126+
success("$yellow Updating your Pdp local cache.$reset");
123127
$retVal = $installer->refresh($arguments);
124-
128+
if (0 === $retVal) {
129+
success("$green Pdp local cache successfully updated. $reset");
130+
} else {
131+
fail("$redbg The command failed to update Pdp local cache successfully. $reset");
132+
}
125133
die($retVal);

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@
6767
"phpcs": "php-cs-fixer fix -v --diff --dry-run --allow-risky=yes --ansi",
6868
"phpstan-src": "phpstan analyse -l max -c phpstan.src.neon src --ansi",
6969
"phpstan-tests": "phpstan analyse -l max -c phpstan.tests.neon tests --ansi",
70+
"phpstan-bin": "phpstan analyse -l max -c phpstan.src.neon bin --ansi",
7071
"phpstan": [
7172
"@phpstan-src",
73+
"@phpstan-bin",
7274
"@phpstan-tests"
7375
],
7476
"phpunit": "phpunit --coverage-text",
@@ -83,8 +85,9 @@
8385
"scripts-descriptions": {
8486
"phpcs": "Runs coding style test suite",
8587
"phpstan": "Runs complete codebase static analysis",
86-
"phpstan-src": "Runs source code static analysis",
87-
"phpstan-test": "Runs test suite static analysis",
88+
"phpstan-src": "Runs PHP stan on the source code",
89+
"phpstan-test": "Runs PHP stan on the test suite",
90+
"phpstan-bin": "Runs PHP stan on the command",
8891
"phpunit": "Runs unit and functional testing",
8992
"test": "Runs full test suite"
9093
},

src/Installer.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Psr\Log\LoggerInterface;
2020
use Psr\SimpleCache\CacheException as PsrCacheException;
2121
use Throwable;
22-
use function array_merge;
22+
use function array_replace;
2323
use function extension_loaded;
2424
use function file_exists;
2525
use function filter_var_array;
@@ -80,7 +80,7 @@ public static function createFromCacheDir(LoggerInterface $logger, string $cache
8080

8181
public function refresh(array $context = []): int
8282
{
83-
$context = filter_var_array(array_merge(self::DEFAULT_CONTEXT, $context), [
83+
$context = filter_var_array(array_replace(self::DEFAULT_CONTEXT, $context), [
8484
self::REFRESH_PSL_KEY => FILTER_VALIDATE_BOOLEAN,
8585
self::REFRESH_PSL_URL_KEY => ['filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW, 'default' => Manager::PSL_URL],
8686
self::REFRESH_RZD_KEY => FILTER_VALIDATE_BOOLEAN,
@@ -96,16 +96,10 @@ public function refresh(array $context = []): int
9696
try {
9797
$retVal = $this->execute($context);
9898
} catch (PsrCacheException $exception) {
99-
$this->logger->error('😓 😓 😓 Your local cache could not be updated. 😓 😓 😓');
100-
$this->logger->error('An error occurred during cache regeneration.');
101-
$this->logger->error('----- Error Message ----');
102-
$this->logger->error($exception->getMessage());
99+
$this->logger->error('Local cache update failed with {exception}', ['exception' => $exception->getMessage()]);
103100
$retVal = 1;
104101
} catch (Throwable $exception) {
105-
$this->logger->error('😓 😓 😓 Your local cache could not be updated. 😓 😓 😓');
106-
$this->logger->error('An error occurred during the update.');
107-
$this->logger->error('----- Error Message ----');
108-
$this->logger->error($exception->getMessage());
102+
$this->logger->error('Local cache update failed with {exception}', ['exception' => $exception->getMessage()]);
109103
$retVal = 1;
110104
}
111105

@@ -121,18 +115,20 @@ public function refresh(array $context = []): int
121115
*/
122116
private function execute(array $arguments = []): int
123117
{
124-
$this->logger->info('Updating your Pdp local cache.');
125-
126118
if ($arguments[self::REFRESH_PSL_KEY]) {
127-
$this->logger->info('Updating your Public Suffix List copy.');
128119
if (!$this->manager->refreshRules($arguments[self::REFRESH_PSL_URL_KEY], $arguments[self::TTL_KEY])) {
129-
$this->logger->error('😓 😓 😓 Your Public Suffix List copy could not be updated. 😓 😓 😓');
130-
$this->logger->error('Please review your settings.');
120+
$this->logger->error('Unable to update the Public Suffix List Cache using {psl_url} with a TTL of {ttl}', [
121+
'psl_url' => $arguments[self::REFRESH_PSL_URL_KEY],
122+
'ttl' => $arguments[self::TTL_KEY],
123+
]);
131124

132125
return 1;
133126
}
134127

135-
$this->logger->info('💪 💪 💪 Your Public Suffix List copy is updated. 💪 💪 💪');
128+
$this->logger->info('Public Suffix List Cache updated for {ttl} using {psl_url}', [
129+
'psl_url' => $arguments[self::REFRESH_PSL_URL_KEY],
130+
'ttl' => $arguments[self::TTL_KEY],
131+
]);
136132
}
137133

138134
if (!$arguments[self::REFRESH_RZD_KEY]) {
@@ -141,13 +137,18 @@ private function execute(array $arguments = []): int
141137

142138
$this->logger->info('Updating your IANA Root Zone Database copy.');
143139
if ($this->manager->refreshTLDs($arguments[self::REFRESH_RZD_URL_KEY], $arguments[self::TTL_KEY])) {
144-
$this->logger->info('💪 💪 💪 Your IANA Root Zone Database copy is updated. 💪 💪 💪');
140+
$this->logger->info('IANA Root Zone Database Cache updated for {ttl} using {rzd_url}', [
141+
'rzd_url' => $arguments[self::REFRESH_RZD_URL_KEY],
142+
'ttl' => $arguments[self::TTL_KEY],
143+
]);
145144

146145
return 0;
147146
}
148147

149-
$this->logger->error('😓 😓 😓 Your IANA Root Zone Database copy could not be updated. 😓 😓 😓');
150-
$this->logger->error('Please review your settings.');
148+
$this->logger->error('Unable to update the IANA Root Zone Database Cache using {rzd_url} with a TTL of {ttl}', [
149+
'rzd_url' => $arguments[self::REFRESH_RZD_URL_KEY],
150+
'ttl' => $arguments[self::TTL_KEY],
151+
]);
151152

152153
return 1;
153154
}
@@ -187,9 +188,15 @@ public static function updateLocalCache(Event $event = null)
187188
$arguments = array_replace($arguments, $event->getArguments());
188189
}
189190

190-
$installer = self::createFromCacheDir(new Logger(), $arguments[Installer::CACHE_DIR_KEY]);
191+
$logger = new Logger();
192+
$installer = self::createFromCacheDir($logger, $arguments[Installer::CACHE_DIR_KEY]);
193+
$logger->info('Updating your Pdp local cache.');
191194
$retVal = $installer->refresh($arguments);
192-
195+
if (0 === $retVal) {
196+
$logger->info('Pdp local cache successfully updated.');
197+
} else {
198+
$logger->error("The command failed to update Pdp local cache successfully.");
199+
}
193200
die($retVal);
194201
}
195202

0 commit comments

Comments
 (0)