Skip to content

Commit d121ceb

Browse files
committed
=fix a bug to allow the command to work with version 5.1 and 5.2
1 parent ea993e2 commit d121ceb

File tree

3 files changed

+110
-5
lines changed

3 files changed

+110
-5
lines changed

src/commands/CreateLanguageCommand.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
namespace CrestApps\CodeGenerator\Commands;
44

55
use File;
6-
use Lang;
76
use Illuminate\Console\Command;
87
use CrestApps\CodeGenerator\Support\Helpers;
98
use CrestApps\CodeGenerator\Traits\CommonCommand;
109
use CrestApps\CodeGenerator\Support\Label;
11-
10+
use CrestApps\CodeGenerator\Support\CrestAppsTranslator;
1211

1312
class CreateLanguageCommand extends Command
1413
{
@@ -82,7 +81,7 @@ protected function registerMessages(array $messages, $language)
8281
{
8382
if( count($messages) > 0)
8483
{
85-
Lang::addLines($messages, $language);
84+
$this->getTranslator()->addLines($messages, $language);
8685
}
8786

8887
return $this;
@@ -98,7 +97,23 @@ protected function registerMessages(array $messages, $language)
9897
*/
9998
protected function isMessageExists($key, $language)
10099
{
101-
return Lang::has($key, $language, false);
100+
return $this->getTranslator()->has($key, $language, false);
101+
}
102+
103+
/**
104+
* Depends on the framework version, it a singleton instance of a translator.
105+
*
106+
* @return CrestApps\CodeGenerator\Support\CrestAppsTranslator | Illuminate\Translation\Translator
107+
*/
108+
protected function getTranslator()
109+
{
110+
111+
if(!$this->isNewerThan('5.3'))
112+
{
113+
return CrestAppsTranslator::getTranslator();
114+
}
115+
116+
return app('translator');
102117
}
103118

104119
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace CrestApps\CodeGenerator\Support;
4+
5+
use Illuminate\Translation\Translator;
6+
use Illuminate\Support\Arr;
7+
8+
class CrestAppsTranslator extends Translator
9+
{
10+
/**
11+
* Add translation lines to the given locale.
12+
*
13+
* @param array $lines
14+
* @param string $locale
15+
* @param string $namespace
16+
* @return void
17+
*/
18+
public function addLines(array $lines, $locale, $namespace = '*')
19+
{
20+
foreach ($lines as $key => $value) {
21+
list($group, $item) = explode('.', $key, 2);
22+
Arr::set($this->loaded, "$namespace.$group.$locale.$item", $value);
23+
}
24+
}
25+
26+
/**
27+
* Adds a new instance of crestapps_translator to the IoC container,
28+
*
29+
* @return CrestApps\CodeGenerator\Support\CrestAppsTranslator
30+
*/
31+
public static function getTranslator()
32+
{
33+
$translator = app('translator');
34+
35+
app()->singleton('crestapps_translator', function ($app) use($translator) {
36+
37+
$trans = new CrestAppsTranslator($translator->getLoader(), $translator->getLocale());
38+
39+
$trans->setFallback($translator->getFallback());
40+
41+
return $trans;
42+
});
43+
44+
return app('crestapps_translator');
45+
}
46+
47+
48+
}

src/traits/CommonCommand.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,63 @@ protected function getFields($fields, $langFile, $fieldsFile = null)
3939
return Helpers::getFields($fields, $langFile);
4040
}
4141

42+
/**
43+
* Evaluates the current version of the framework to see if it >= a giving version.
44+
*
45+
* @param $version
46+
*
47+
* @return bool
48+
*/
49+
protected function isNewerThan($version = '5.3')
50+
{
51+
return (App::VERSION() >= $version);
52+
}
53+
4254
/**
4355
* Gets the correct routes fullname based on current framework version.
4456
*
4557
* @return string
4658
*/
4759
protected function getRoutesFileName()
4860
{
49-
if (App::VERSION() >= '5.3')
61+
if ($this->isNewerThan())
5062
{
5163
return base_path('routes/web.php');
5264
}
5365

5466
return app_path('Http/routes.php');
5567
}
5668

69+
/**
70+
* Gets all command's arguments depending on the current framework version.
71+
*
72+
* @return string
73+
*/
74+
public function arguments()
75+
{
76+
if ($this->isNewerThan())
77+
{
78+
return parent::arguments();
79+
}
80+
81+
return parent::argument();
82+
}
83+
84+
/**
85+
* Gets all command's options depending on the current framework version.
86+
*
87+
* @return string
88+
*/
89+
public function options()
90+
{
91+
if ($this->isNewerThan())
92+
{
93+
return parent::options();
94+
}
95+
96+
return parent::option();
97+
}
98+
5799
/**
58100
* It Replaces the view names in a giving stub
59101
*

0 commit comments

Comments
 (0)