Skip to content

Commit aaa8b41

Browse files
committed
generate default language file
1 parent a58396c commit aaa8b41

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/Commands/Generators/NewShieldOauthGenerator.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public function run(array $params): int
118118
return 1;
119119
}
120120

121+
// Default language file is required
122+
if ($this->createLang($class) !== 0) {
123+
return 1;
124+
}
125+
121126
// @TODO execute() is deprecated in CI v4.3.0.
122127
$this->execute($params); // @phpstan-ignore-line suppress deprecated error.
123128

@@ -157,6 +162,56 @@ private function updateConfig($className): int
157162
return 0;
158163
}
159164

165+
/**
166+
* Create or update language file for English (en).
167+
* Other languages should be translated by the
168+
* developer where necessary.
169+
*
170+
* @param string $className
171+
*/
172+
private function createLang($className): int
173+
{
174+
// Set the Oauth key, name and actual content
175+
$oauthKey = strtolower(str_replace('OAuth', '', $className));
176+
$oauthName = ucfirst(str_replace('OAuth', '', $className));
177+
$oauthContent = "'{$oauthName}' => [
178+
'not_allow' => 'Now you can\\'t login or register with {$oauthName}!',
179+
'{$oauthKey}' => '{$oauthName}',
180+
],";
181+
// Update if file exists
182+
$file = 'Language/en/ShieldOAuthLang.php';
183+
$path = $this->distPath . $file;
184+
if (is_file($this->distPath . $file)) {
185+
// Code addition setup
186+
$pattern = '/(' . preg_quote('return [', '/') . ')/u';
187+
$replace = '$1' . "\n " . $oauthContent;
188+
189+
$this->add($file, $oauthContent, $pattern, $replace);
190+
191+
return 0;
192+
}
193+
194+
// This is a new file, prepare the contents
195+
$content = "<?php \n\n";
196+
$content .= "return [\n";
197+
$content .= " {$oauthContent}\n";
198+
$content .= "];\n";
199+
$cleanPath = clean_path($path);
200+
$directory = dirname($path);
201+
if (! is_dir($directory)) {
202+
mkdir($directory, 0777, true);
203+
}
204+
205+
if (write_file($path, $content)) {
206+
CLI::write(CLI::color(' Created: ', 'green') . $cleanPath);
207+
208+
return 0;
209+
}
210+
CLI::error(" Error creating {$cleanPath}.");
211+
212+
return 1;
213+
}
214+
160215
/**
161216
* @param string $code Code to add.
162217
* @param string $file Relative file path like 'Controllers/BaseController.php'.

0 commit comments

Comments
 (0)