@@ -21,16 +21,25 @@ discard block |
||
| 21 | 21 | $this->argumentFormatLoader = new ArgumentFormatLoader($fs); |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | + /** |
|
| 25 | + * @param string $name |
|
| 26 | + */ |
|
| 24 | 27 | protected function getTemplate($name) |
| 25 | 28 | { |
| 26 | 29 | return $this->templates->load($name); |
| 27 | 30 | } |
| 28 | 31 | |
| 32 | + /** |
|
| 33 | + * @param string $name |
|
| 34 | + */ |
|
| 29 | 35 | protected function getArgumentParser($name){ |
| 30 | 36 | $format = $this->argumentFormatLoader->load($name); |
| 31 | 37 | return new ArgumentParser($format); |
| 32 | 38 | } |
| 33 | 39 | |
| 40 | + /** |
|
| 41 | + * @param string $content |
|
| 42 | + */ |
|
| 34 | 43 | protected function save($content, $path, $name, $force = false) |
| 35 | 44 | { |
| 36 | 45 | if (!$force && $this->fs->exists($path) && $this->input->hasOption('force') && !$this->option('force')) { |
@@ -49,6 +58,9 @@ discard block |
||
| 49 | 58 | } |
| 50 | 59 | } |
| 51 | 60 | |
| 61 | + /** |
|
| 62 | + * @param integer $n |
|
| 63 | + */ |
|
| 52 | 64 | protected function spaces($n) |
| 53 | 65 | { |
| 54 | 66 | return str_repeat(' ', $n); |
@@ -10,10 +10,10 @@ discard block |
||
| 10 | 10 | class BaseCommand extends Command { |
| 11 | 11 | |
| 12 | 12 | protected $fs; |
| 13 | - protected $templates; |
|
| 13 | + protected $templates; |
|
| 14 | 14 | |
| 15 | - public function __construct(Filesystem $fs) |
|
| 16 | - { |
|
| 15 | + public function __construct(Filesystem $fs) |
|
| 16 | + { |
|
| 17 | 17 | parent::__construct(); |
| 18 | 18 | |
| 19 | 19 | $this->fs = $fs; |
@@ -56,11 +56,11 @@ discard block |
||
| 56 | 56 | |
| 57 | 57 | protected function getNamespace($path = false) |
| 58 | 58 | { |
| 59 | - if (! $path) { |
|
| 60 | - $path = $this->option('path'); |
|
| 61 | - } |
|
| 59 | + if (! $path) { |
|
| 60 | + $path = $this->option('path'); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - return str_replace(' ', '\\', ucwords(trim(str_replace('/', ' ', $path)))); |
|
| 63 | + return str_replace(' ', '\\', ucwords(trim(str_replace('/', ' ', $path)))); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | } |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | return $this->templates->load($name); |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - protected function getArgumentParser($name){ |
|
| 29 | + protected function getArgumentParser($name) { |
|
| 30 | 30 | $format = $this->argumentFormatLoader->load($name); |
| 31 | 31 | return new ArgumentParser($format); |
| 32 | 32 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | |
| 57 | 57 | protected function getNamespace($path = false) |
| 58 | 58 | { |
| 59 | - if (! $path) { |
|
| 59 | + if (!$path) { |
|
| 60 | 60 | $path = $this->option('path'); |
| 61 | 61 | } |
| 62 | 62 | |
@@ -19,11 +19,11 @@ |
||
| 19 | 19 | |
| 20 | 20 | public function load($name) |
| 21 | 21 | { |
| 22 | - if(! isset($this->loaded[$name])){ |
|
| 22 | + if (!isset($this->loaded[$name])) { |
|
| 23 | 23 | $path = __DIR__ . "/../../templates/{$name}.wnt"; |
| 24 | 24 | try { |
| 25 | 25 | $this->loaded[$name] = $this->fs->get($path); |
| 26 | - } catch(\Exception $e) { |
|
| 26 | + } catch (\Exception $e) { |
|
| 27 | 27 | throw new TemplateException("Unable to read the file '{$path}'"); |
| 28 | 28 | } |
| 29 | 29 | } |
@@ -7,27 +7,27 @@ |
||
| 7 | 7 | |
| 8 | 8 | class TemplateLoader { |
| 9 | 9 | |
| 10 | - protected $fs; |
|
| 10 | + protected $fs; |
|
| 11 | 11 | |
| 12 | - protected $loaded; |
|
| 12 | + protected $loaded; |
|
| 13 | 13 | |
| 14 | - public function __construct(Filesystem $fs) |
|
| 15 | - { |
|
| 16 | - $this->fs = $fs; |
|
| 17 | - $this->loaded = []; |
|
| 18 | - } |
|
| 14 | + public function __construct(Filesystem $fs) |
|
| 15 | + { |
|
| 16 | + $this->fs = $fs; |
|
| 17 | + $this->loaded = []; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - public function load($name) |
|
| 21 | - { |
|
| 22 | - if(! isset($this->loaded[$name])){ |
|
| 23 | - $path = __DIR__ . "/../../templates/{$name}.wnt"; |
|
| 24 | - try { |
|
| 25 | - $this->loaded[$name] = $this->fs->get($path); |
|
| 26 | - } catch(\Exception $e) { |
|
| 27 | - throw new TemplateException("Unable to read the file '{$path}'"); |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - return new Template($this, $this->loaded[$name]); |
|
| 31 | - } |
|
| 20 | + public function load($name) |
|
| 21 | + { |
|
| 22 | + if(! isset($this->loaded[$name])){ |
|
| 23 | + $path = __DIR__ . "/../../templates/{$name}.wnt"; |
|
| 24 | + try { |
|
| 25 | + $this->loaded[$name] = $this->fs->get($path); |
|
| 26 | + } catch(\Exception $e) { |
|
| 27 | + throw new TemplateException("Unable to read the file '{$path}'"); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + return new Template($this, $this->loaded[$name]); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | } |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | public function with($data = []) |
| 35 | 35 | { |
| 36 | - if($data) |
|
| 36 | + if ($data) |
|
| 37 | 37 | $this->dirty = true; |
| 38 | 38 | foreach ($data as $key => $value) { |
| 39 | 39 | $this->data[$key] = $value; |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | public function get() |
| 45 | 45 | { |
| 46 | - if($this->dirty){ |
|
| 46 | + if ($this->dirty) { |
|
| 47 | 47 | $this->compile(); |
| 48 | 48 | $this->dirty = false; |
| 49 | 49 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | public function compile() |
| 54 | 54 | { |
| 55 | 55 | $this->compiled = $this->text; |
| 56 | - foreach($this->data as $key => $value){ |
|
| 56 | + foreach ($this->data as $key => $value) { |
|
| 57 | 57 | $this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled); |
| 58 | 58 | } |
| 59 | 59 | return $this; |
@@ -33,8 +33,9 @@ |
||
| 33 | 33 | |
| 34 | 34 | public function with($data = []) |
| 35 | 35 | { |
| 36 | - if($data) |
|
| 37 | - $this->dirty = true; |
|
| 36 | + if($data) { |
|
| 37 | + $this->dirty = true; |
|
| 38 | + } |
|
| 38 | 39 | foreach ($data as $key => $value) { |
| 39 | 40 | $this->data[$key] = $value; |
| 40 | 41 | } |
@@ -5,58 +5,58 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Template { |
| 7 | 7 | |
| 8 | - protected $loader; |
|
| 9 | - |
|
| 10 | - protected $text; |
|
| 11 | - |
|
| 12 | - protected $data; |
|
| 13 | - |
|
| 14 | - protected $compiled; |
|
| 15 | - |
|
| 16 | - protected $dirty; |
|
| 17 | - |
|
| 18 | - public function __construct(TemplateLoader $loader, $text) |
|
| 19 | - { |
|
| 20 | - $this->loader = $loader; |
|
| 21 | - $this->text = $text; |
|
| 22 | - $this->compiled = ''; |
|
| 23 | - $this->data = []; |
|
| 24 | - $this->dirty = true; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public function clean() |
|
| 28 | - { |
|
| 29 | - $this->data = []; |
|
| 30 | - $this->dirty = false; |
|
| 31 | - return $this; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - public function with($data = []) |
|
| 35 | - { |
|
| 36 | - if($data) |
|
| 37 | - $this->dirty = true; |
|
| 38 | - foreach ($data as $key => $value) { |
|
| 39 | - $this->data[$key] = $value; |
|
| 40 | - } |
|
| 41 | - return $this; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - public function get() |
|
| 45 | - { |
|
| 46 | - if($this->dirty){ |
|
| 47 | - $this->compile(); |
|
| 48 | - $this->dirty = false; |
|
| 49 | - } |
|
| 50 | - return $this->compiled; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function compile() |
|
| 54 | - { |
|
| 55 | - $this->compiled = $this->text; |
|
| 56 | - foreach($this->data as $key => $value){ |
|
| 57 | - $this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled); |
|
| 58 | - } |
|
| 59 | - return $this; |
|
| 60 | - } |
|
| 8 | + protected $loader; |
|
| 9 | + |
|
| 10 | + protected $text; |
|
| 11 | + |
|
| 12 | + protected $data; |
|
| 13 | + |
|
| 14 | + protected $compiled; |
|
| 15 | + |
|
| 16 | + protected $dirty; |
|
| 17 | + |
|
| 18 | + public function __construct(TemplateLoader $loader, $text) |
|
| 19 | + { |
|
| 20 | + $this->loader = $loader; |
|
| 21 | + $this->text = $text; |
|
| 22 | + $this->compiled = ''; |
|
| 23 | + $this->data = []; |
|
| 24 | + $this->dirty = true; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public function clean() |
|
| 28 | + { |
|
| 29 | + $this->data = []; |
|
| 30 | + $this->dirty = false; |
|
| 31 | + return $this; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + public function with($data = []) |
|
| 35 | + { |
|
| 36 | + if($data) |
|
| 37 | + $this->dirty = true; |
|
| 38 | + foreach ($data as $key => $value) { |
|
| 39 | + $this->data[$key] = $value; |
|
| 40 | + } |
|
| 41 | + return $this; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + public function get() |
|
| 45 | + { |
|
| 46 | + if($this->dirty){ |
|
| 47 | + $this->compile(); |
|
| 48 | + $this->dirty = false; |
|
| 49 | + } |
|
| 50 | + return $this->compiled; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function compile() |
|
| 54 | + { |
|
| 55 | + $this->compiled = $this->text; |
|
| 56 | + foreach($this->data as $key => $value){ |
|
| 57 | + $this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled); |
|
| 58 | + } |
|
| 59 | + return $this; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | } |
@@ -3,10 +3,10 @@ |
||
| 3 | 3 | |
| 4 | 4 | class ControllerRestActionsCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:controller:rest-actions |
|
| 6 | + protected $signature = 'wn:controller:rest-actions |
|
| 7 | 7 | {--force= : override the existing files}'; |
| 8 | 8 | |
| 9 | - protected $description = 'Generates REST actions trait to use into controllers'; |
|
| 9 | + protected $description = 'Generates REST actions trait to use into controllers'; |
|
| 10 | 10 | |
| 11 | 11 | public function handle() |
| 12 | 12 | { |
@@ -3,13 +3,13 @@ |
||
| 3 | 3 | |
| 4 | 4 | class SeederCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:seeder |
|
| 6 | + protected $signature = 'wn:seeder |
|
| 7 | 7 | {model : full qualified name of the model.} |
| 8 | 8 | {--count=10 : number of elements to add in database.} |
| 9 | 9 | {--force= : override the existing files} |
| 10 | 10 | '; |
| 11 | 11 | |
| 12 | - protected $description = 'Generates a seeder'; |
|
| 12 | + protected $description = 'Generates a seeder'; |
|
| 13 | 13 | |
| 14 | 14 | public function handle() |
| 15 | 15 | { |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | |
| 4 | 4 | class FactoryCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:factory |
|
| 6 | + protected $signature = 'wn:factory |
|
| 7 | 7 | {model : full qualified name of the model.} |
| 8 | 8 | {--fields= : the fields to generate.} |
| 9 | 9 | {--file= : the factories file.} |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | {--force= : override the existing files} |
| 12 | 12 | '; |
| 13 | 13 | |
| 14 | - protected $description = 'Generates a model factory'; |
|
| 14 | + protected $description = 'Generates a model factory'; |
|
| 15 | 15 | |
| 16 | 16 | public function handle() |
| 17 | 17 | { |
@@ -19,9 +19,9 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | $file = $this->getFile(); |
| 21 | 21 | |
| 22 | - $content = ! $this->fs->exists($file) ? '' : $this->fs->get($file); |
|
| 22 | + $content = !$this->fs->exists($file) ? '' : $this->fs->get($file); |
|
| 23 | 23 | |
| 24 | - $content .= $this->getTemplate(! $this->fs->exists($file) ? 'factory-create' : 'factory') |
|
| 24 | + $content .= $this->getTemplate(!$this->fs->exists($file) ? 'factory-create' : 'factory') |
|
| 25 | 25 | ->with([ |
| 26 | 26 | 'model' => $model, |
| 27 | 27 | 'factory_fields' => $this->getFieldsContent() |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | protected function getFile() |
| 35 | 35 | { |
| 36 | 36 | $file = $this->option('file'); |
| 37 | - if(! $file){ |
|
| 37 | + if (!$file) { |
|
| 38 | 38 | $file = './database/factories/ModelFactory.php'; |
| 39 | 39 | } |
| 40 | 40 | return $file; |
@@ -46,12 +46,12 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | $fields = $this->option('fields'); |
| 48 | 48 | |
| 49 | - if($fields){ |
|
| 50 | - if(! $this->option('parsed')){ |
|
| 49 | + if ($fields) { |
|
| 50 | + if (!$this->option('parsed')) { |
|
| 51 | 51 | $fields = $this->getArgumentParser('factory-fields')->parse($fields); |
| 52 | 52 | } |
| 53 | 53 | $template = $this->getTemplate('factory/field'); |
| 54 | - foreach($fields as $field){ |
|
| 54 | + foreach ($fields as $field) { |
|
| 55 | 55 | $content[] = $template->with($field)->get(); |
| 56 | 56 | } |
| 57 | 57 | $content = implode(PHP_EOL, $content); |
@@ -3,14 +3,14 @@ |
||
| 3 | 3 | |
| 4 | 4 | class PivotSeederCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:pivot-seeder |
|
| 6 | + protected $signature = 'wn:pivot-seeder |
|
| 7 | 7 | {model1 : Name of the first model or table} |
| 8 | 8 | {model2 : Name of the second model or table} |
| 9 | 9 | {--count=10 : number of elements to add in database.} |
| 10 | 10 | {--force= : override the existing files} |
| 11 | 11 | '; |
| 12 | 12 | |
| 13 | - protected $description = 'Generates seeder for pivot table'; |
|
| 13 | + protected $description = 'Generates seeder for pivot table'; |
|
| 14 | 14 | |
| 15 | 15 | public function handle() |
| 16 | 16 | { |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | protected function getSeederName($resources) { |
| 48 | - $resources = array_map(function($resource){ |
|
| 48 | + $resources = array_map(function($resource) { |
|
| 49 | 49 | return ucwords(camel_case($resource)); |
| 50 | 50 | }, $resources); |
| 51 | 51 | return implode('', $resources) . 'TableSeeder'; |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | |
| 4 | 4 | class PivotTableCommand extends BaseCommand { |
| 5 | 5 | |
| 6 | - protected $signature = 'wn:pivot-table |
|
| 6 | + protected $signature = 'wn:pivot-table |
|
| 7 | 7 | {model1 : Name of the first model or table} |
| 8 | 8 | {model2 : Name of the second model or table} |
| 9 | 9 | {--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.} |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | {--force= : override the existing files} |
| 12 | 12 | '; |
| 13 | 13 | |
| 14 | - protected $description = 'Generates creation migration for a pivot table'; |
|
| 14 | + protected $description = 'Generates creation migration for a pivot table'; |
|
| 15 | 15 | |
| 16 | 16 | protected $tables; |
| 17 | 17 | |
@@ -41,14 +41,14 @@ |
||
| 41 | 41 | |
| 42 | 42 | protected function schema() |
| 43 | 43 | { |
| 44 | - return implode(' ', array_map(function($table){ |
|
| 44 | + return implode(' ', array_map(function($table) { |
|
| 45 | 45 | return $table . '_id:integer:unsigned:index'; |
| 46 | 46 | }, $this->tables)); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | protected function keys() |
| 50 | 50 | { |
| 51 | - return implode(' ', array_map(function($table){ |
|
| 51 | + return implode(' ', array_map(function($table) { |
|
| 52 | 52 | return $table . '_id'; |
| 53 | 53 | }, $this->tables)); |
| 54 | 54 | } |
@@ -19,15 +19,15 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | public function load($name) |
| 21 | 21 | { |
| 22 | - if(! isset($this->loaded[$name])){ |
|
| 22 | + if (!isset($this->loaded[$name])) { |
|
| 23 | 23 | $path = __DIR__ . "/../../formats/{$name}.json"; |
| 24 | 24 | $json = ""; |
| 25 | 25 | try { |
| 26 | 26 | $json = json_decode($this->fs->get($path)); |
| 27 | - } catch(\Exception $e) { |
|
| 27 | + } catch (\Exception $e) { |
|
| 28 | 28 | throw new ArgumentFormatException("Unable to read the file '{$path}'"); |
| 29 | 29 | } |
| 30 | - if (json_last_error() !== JSON_ERROR_NONE){ |
|
| 30 | + if (json_last_error() !== JSON_ERROR_NONE) { |
|
| 31 | 31 | throw new ArgumentFormatException("Error while parsing the JSON file '{$path}'"); |
| 32 | 32 | } |
| 33 | 33 | $this->loaded[$name] = $this->buildFormat($json); |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | return $format; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - protected function fillObject($obj, $firstLevel=false) |
|
| 84 | + protected function fillObject($obj, $firstLevel = false) |
|
| 85 | 85 | { |
| 86 | 86 | $format = new ArgumentFormat; |
| 87 | 87 | |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | if (isset($obj->separator)) { |
| 108 | 108 | $format->separator = $obj->separator; |
| 109 | 109 | } elseif ($firstLevel) { |
| 110 | - $format->separator = ($format->type === 'object') ? ':':','; |
|
| 110 | + $format->separator = ($format->type === 'object') ? ':' : ','; |
|
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |
@@ -7,140 +7,140 @@ |
||
| 7 | 7 | |
| 8 | 8 | class ArgumentFormatLoader { |
| 9 | 9 | |
| 10 | - protected $fs; |
|
| 11 | - |
|
| 12 | - protected $loaded; |
|
| 13 | - |
|
| 14 | - public function __construct(Filesystem $fs) |
|
| 15 | - { |
|
| 16 | - $this->fs = $fs; |
|
| 17 | - $this->loaded = []; |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - public function load($name) |
|
| 21 | - { |
|
| 22 | - if(! isset($this->loaded[$name])){ |
|
| 23 | - $path = __DIR__ . "/../../formats/{$name}.json"; |
|
| 24 | - $json = ""; |
|
| 25 | - try { |
|
| 26 | - $json = json_decode($this->fs->get($path)); |
|
| 27 | - } catch(\Exception $e) { |
|
| 28 | - throw new ArgumentFormatException("Unable to read the file '{$path}'"); |
|
| 29 | - } |
|
| 30 | - if (json_last_error() !== JSON_ERROR_NONE){ |
|
| 31 | - throw new ArgumentFormatException("Error while parsing the JSON file '{$path}'"); |
|
| 32 | - } |
|
| 33 | - $this->loaded[$name] = $this->buildFormat($json); |
|
| 34 | - } |
|
| 35 | - return $this->loaded[$name]; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - protected function buildFormat($obj) |
|
| 39 | - { |
|
| 40 | - return $this->fillFirstLevel($obj); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - protected function fillFirstLevel($obj) |
|
| 44 | - { |
|
| 45 | - return $this->fill($obj, true); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - protected function fill($obj, $firstLevel = false) |
|
| 49 | - { |
|
| 50 | - if (is_string($obj)) { |
|
| 51 | - return $this->fillString($obj, $firstLevel); |
|
| 52 | - } else { |
|
| 53 | - return $this->fillObject($obj, $firstLevel); |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - protected function fillString($string, $firstLevel = false) |
|
| 58 | - { |
|
| 59 | - list($name, $type, $isArray) = $this->parseName($string); |
|
| 10 | + protected $fs; |
|
| 11 | + |
|
| 12 | + protected $loaded; |
|
| 13 | + |
|
| 14 | + public function __construct(Filesystem $fs) |
|
| 15 | + { |
|
| 16 | + $this->fs = $fs; |
|
| 17 | + $this->loaded = []; |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + public function load($name) |
|
| 21 | + { |
|
| 22 | + if(! isset($this->loaded[$name])){ |
|
| 23 | + $path = __DIR__ . "/../../formats/{$name}.json"; |
|
| 24 | + $json = ""; |
|
| 25 | + try { |
|
| 26 | + $json = json_decode($this->fs->get($path)); |
|
| 27 | + } catch(\Exception $e) { |
|
| 28 | + throw new ArgumentFormatException("Unable to read the file '{$path}'"); |
|
| 29 | + } |
|
| 30 | + if (json_last_error() !== JSON_ERROR_NONE){ |
|
| 31 | + throw new ArgumentFormatException("Error while parsing the JSON file '{$path}'"); |
|
| 32 | + } |
|
| 33 | + $this->loaded[$name] = $this->buildFormat($json); |
|
| 34 | + } |
|
| 35 | + return $this->loaded[$name]; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + protected function buildFormat($obj) |
|
| 39 | + { |
|
| 40 | + return $this->fillFirstLevel($obj); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + protected function fillFirstLevel($obj) |
|
| 44 | + { |
|
| 45 | + return $this->fill($obj, true); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + protected function fill($obj, $firstLevel = false) |
|
| 49 | + { |
|
| 50 | + if (is_string($obj)) { |
|
| 51 | + return $this->fillString($obj, $firstLevel); |
|
| 52 | + } else { |
|
| 53 | + return $this->fillObject($obj, $firstLevel); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + protected function fillString($string, $firstLevel = false) |
|
| 58 | + { |
|
| 59 | + list($name, $type, $isArray) = $this->parseName($string); |
|
| 60 | 60 | |
| 61 | - $format = new ArgumentFormat; |
|
| 62 | - $format->name = $name; |
|
| 61 | + $format = new ArgumentFormat; |
|
| 62 | + $format->name = $name; |
|
| 63 | 63 | |
| 64 | - if ($isArray) { |
|
| 65 | - $format->type = 'array'; |
|
| 66 | - $subFormat = new ArgumentFormat; |
|
| 67 | - $subFormat->type = $type ?: 'string'; |
|
| 68 | - $format->format = $subFormat; |
|
| 69 | - } else { |
|
| 70 | - $format->type = 'string'; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if ($firstLevel) { |
|
| 74 | - if ($format->type === 'object') { |
|
| 75 | - $format->separator = ':'; |
|
| 76 | - } elseif ($format->type === 'array') { |
|
| 77 | - $format->separator = ','; |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - return $format; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - protected function fillObject($obj, $firstLevel=false) |
|
| 85 | - { |
|
| 86 | - $format = new ArgumentFormat; |
|
| 87 | - |
|
| 88 | - // Resolve type from name |
|
| 89 | - if (isset($obj->name)) { |
|
| 90 | - list($name, $type) = $this->parseName($obj->name); |
|
| 91 | - $format->name = $name; |
|
| 92 | - $format->type = $type; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // Fill type if set |
|
| 96 | - if (isset($obj->type)) { |
|
| 97 | - $format->type = $obj->type; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Fill default if set |
|
| 101 | - if (isset($obj->default)) { |
|
| 102 | - $format->default = $obj->default; |
|
| 103 | - } |
|
| 64 | + if ($isArray) { |
|
| 65 | + $format->type = 'array'; |
|
| 66 | + $subFormat = new ArgumentFormat; |
|
| 67 | + $subFormat->type = $type ?: 'string'; |
|
| 68 | + $format->format = $subFormat; |
|
| 69 | + } else { |
|
| 70 | + $format->type = 'string'; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if ($firstLevel) { |
|
| 74 | + if ($format->type === 'object') { |
|
| 75 | + $format->separator = ':'; |
|
| 76 | + } elseif ($format->type === 'array') { |
|
| 77 | + $format->separator = ','; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + return $format; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + protected function fillObject($obj, $firstLevel=false) |
|
| 85 | + { |
|
| 86 | + $format = new ArgumentFormat; |
|
| 87 | + |
|
| 88 | + // Resolve type from name |
|
| 89 | + if (isset($obj->name)) { |
|
| 90 | + list($name, $type) = $this->parseName($obj->name); |
|
| 91 | + $format->name = $name; |
|
| 92 | + $format->type = $type; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // Fill type if set |
|
| 96 | + if (isset($obj->type)) { |
|
| 97 | + $format->type = $obj->type; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Fill default if set |
|
| 101 | + if (isset($obj->default)) { |
|
| 102 | + $format->default = $obj->default; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - // Set separator, default to ':' for objects and ',' for arrays in first level |
|
| 106 | - if (in_array($format->type, ['object', 'array'])) { |
|
| 107 | - if (isset($obj->separator)) { |
|
| 108 | - $format->separator = $obj->separator; |
|
| 109 | - } elseif ($firstLevel) { |
|
| 110 | - $format->separator = ($format->type === 'object') ? ':':','; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - // Build format recursively |
|
| 115 | - if (isset($obj->fields)) { |
|
| 116 | - if ($firstLevel) { |
|
| 117 | - if (is_array($obj->fields)) { |
|
| 118 | - $format->format = array_map([$this, 'fillFirstLevel'], $obj->fields); |
|
| 119 | - } else { |
|
| 120 | - $format->format = $this->fill($obj->fields, true); |
|
| 121 | - } |
|
| 122 | - } else { |
|
| 123 | - if (is_array($obj->fields)) { |
|
| 124 | - $format->format = array_map([$this, 'fill'], $obj->fields); |
|
| 125 | - } else { |
|
| 126 | - $format->format = $this->fill($obj->fields); |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return $format; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - protected function parseName($name) |
|
| 135 | - { |
|
| 136 | - $pattern = '/^(?P<attr>\w+)(\[(?P<type>\w+)?\])?/'; |
|
| 137 | - preg_match($pattern, $name, $matches); |
|
| 138 | - |
|
| 139 | - $attr = $matches['attr']; |
|
| 140 | - $type = empty($matches['type']) ? null : $matches['type']; |
|
| 141 | - $isArray = (isset($matches[2][0]) && $matches[2][0] === '['); |
|
| 105 | + // Set separator, default to ':' for objects and ',' for arrays in first level |
|
| 106 | + if (in_array($format->type, ['object', 'array'])) { |
|
| 107 | + if (isset($obj->separator)) { |
|
| 108 | + $format->separator = $obj->separator; |
|
| 109 | + } elseif ($firstLevel) { |
|
| 110 | + $format->separator = ($format->type === 'object') ? ':':','; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // Build format recursively |
|
| 115 | + if (isset($obj->fields)) { |
|
| 116 | + if ($firstLevel) { |
|
| 117 | + if (is_array($obj->fields)) { |
|
| 118 | + $format->format = array_map([$this, 'fillFirstLevel'], $obj->fields); |
|
| 119 | + } else { |
|
| 120 | + $format->format = $this->fill($obj->fields, true); |
|
| 121 | + } |
|
| 122 | + } else { |
|
| 123 | + if (is_array($obj->fields)) { |
|
| 124 | + $format->format = array_map([$this, 'fill'], $obj->fields); |
|
| 125 | + } else { |
|
| 126 | + $format->format = $this->fill($obj->fields); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return $format; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + protected function parseName($name) |
|
| 135 | + { |
|
| 136 | + $pattern = '/^(?P<attr>\w+)(\[(?P<type>\w+)?\])?/'; |
|
| 137 | + preg_match($pattern, $name, $matches); |
|
| 138 | + |
|
| 139 | + $attr = $matches['attr']; |
|
| 140 | + $type = empty($matches['type']) ? null : $matches['type']; |
|
| 141 | + $isArray = (isset($matches[2][0]) && $matches[2][0] === '['); |
|
| 142 | 142 | |
| 143 | - return [$attr, $type, $isArray]; |
|
| 144 | - } |
|
| 143 | + return [$attr, $type, $isArray]; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | 146 | } |