AbstractCompiler::replaceFieldStrings()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Scaffolder\Compilers;
4
5
use Scaffolder\Compilers\Support\FileToCompile;
6
use Illuminate\Support\Facades\File;
7
use Scaffolder\Support\Json;
8
use stdClass ;
9
10
abstract class AbstractCompiler
11
{
12
	protected $cachePrefix ;
13
	protected $stubFilename;
14
15
	protected $stub;
16
	protected $scaffolderConfig ;
17
	protected $modelName ;
18
	protected $modelData ;
19
	protected $stubsDirectory ;
20
21
	protected $eagerTable ;
22
23
	const CACHE_EXT = '.scf';
24
25
	public function __construct($scaffolderConfig, $modelData = null)
26
	{
27
		$this->modelName = isset($modelData->modelName) ? $modelData->modelName : null  ;
28
		$this->modelData = $modelData ;
29
		$this->scaffolderConfig = $scaffolderConfig ;
30
31
		$this->stub = File::get($this->stubsDirectory . $this->stubFilename );
32
	}
33
34
	/**
35
	 * Compiles .
36
	 *
37
	 * @param null $extra
38
	 *
39
	 * @return string
40
	 */
41
	public function compile($extra = null)
42
	{
43
		if (File::exists(base_path('scaffolder-config/cache/' . $this->cachePrefix  . $this->modelData->modelHash . self::CACHE_EXT)))
44
		{
45
			return $this->store(new FileToCompile(true, $this->modelData->modelHash));
46
		}
47
		else
48
		{
49
50
			return $this->replacePrimaryKey()
51
						->replaceClassName()
52
						->replaceTableName()
53
						->replaceRoutePrefix()
54
						->replaceAndStore();
55
		}
56
	}
57
58
	/**
59
	 * Replace and store the Stub.
60
	 *
61
	 * @return string
62
	 */
63
	abstract protected function replaceAndStore();
64
65
	/**
66
	 * Store the compiled stub.
67
	 *
68
	 * @param string $eagerTable
69
	 *
70
	 * @return string
71
	 */
72
	protected function setEagerTable($eagerTable){
73
		$this->eagerTable = $eagerTable.'.';
74
	}
75
76
	/**
77
	 * Store the compiled stub.
78
	 *
79
	 * @param FileToCompile $fileToCompile
80
	 *
81
	 * @return string
82
	 */
83
	protected function store(FileToCompile $fileToCompile)
84
	{
85
		$path = $this->getOutputFilename();
86
87
		// Store in cache
88
		if ($fileToCompile->cached)
89
		{
90
			File::copy(base_path('scaffolder-config/cache/' . $this->cachePrefix . $fileToCompile->hash . self::CACHE_EXT), $path);
91
		}
92
		else
93
		{
94
			File::put(base_path('scaffolder-config/cache/' . $this->cachePrefix . $fileToCompile->hash . self::CACHE_EXT), $this->stub);
95
			File::copy(base_path('scaffolder-config/cache/' . $this->cachePrefix . $fileToCompile->hash . self::CACHE_EXT), $path);
96
		}
97
98
		return $path;
99
	}
100
101
	/**
102
	 * Get output filename
103
	 *
104
	 *
105
	 * @return $this
106
	 */
107
	abstract protected function getOutputFilename();
108
109
	/**
110
	 * Replace the primary key.
111
	 *
112
	 * @param $this->modelData
113
	 */
114
	protected function replacePrimaryKey()
115
	{
116
		
117
		$primaryKey = $this->getPrimaryKeyField()->name;
118
119
		$this->stub = str_replace('{{primary_key}}', $primaryKey, $this->stub);
120
121
		return $this;
122
	}
123
124 View Code Duplication
	protected function getPrimaryKeyField(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
		$primaryKey = new stdClass;
126
		$primaryKey->name = "id" ;
127
		$primaryKey->index = "primary" ;
128
		$primaryKey->declared =  false ;
129
		$primaryKey->type = new stdClass ;
130
		$primaryKey->type->ui = 'label' ;
131
		$primaryKey->type->db = 'integer' ;
132
		$primaryKey->foreignKey = [];
133
		$primaryKey->validations = "required" ;
134
135
		foreach ($this->modelData->fields as $field)
136
		{
137
			if ($field->index == 'primary')
138
			{
139
				$primaryKey = $field ;
140
				break;
141
			}
142
		}
143
144
		return $primaryKey ;
145
	}
146
147
	/**
148
	 * Replace the class name.
149
	 *
150
	 *
151
	 * @return $this
152
	 */
153
	protected function replaceClassName()
154
	{
155
		$this->stub = str_replace('{{class_name}}', $this->modelName, $this->stub);
156
		$this->stub = str_replace('{{class_name_lw}}', strtolower($this->modelName), $this->stub);
157
158
		return $this;
159
	}
160
161
	/**
162
	 * Replace the table name.
163
	 *
164
	 * @return $this
165
	 */
166
	protected function replaceTableName()
167
	{
168
		$this->stub = str_replace('{{table_name}}', $this->modelData->tableName, $this->stub);
169
170
		return $this;
171
	}
172
173
	/**
174
	 * Replace the namespace.
175
	 *
176
	 * @return $this
177
	 */
178
	protected function replaceNamespace()
179
	{
180
		$this->stub = str_replace('{{namespace}}', $this->scaffolderConfig->generator->namespaces->models, $this->stub);
181
182
		return $this;
183
	}
184
185
	/**
186
	 * Replace the foreign strings by field.
187
	 *
188
	 * @param stdClass $field
189
	 * @param string $originalStubPart
190
	 *
191
	 * @return $this
192
	 */
193
	protected function replaceForeingStrings($field,  $originalStubPart){
194
		$replaceStub = str_replace('{{foreign_table}}', $field->foreignKey->table, $originalStubPart);
195
		$replaceStub = str_replace('{{table_name}}', $this->modelData->tableName, $replaceStub);
196
		$replaceStub = str_replace('{{foreign_field}}', $field->foreignKey->field, $replaceStub);
197
		$replaceStub = str_replace('{{foreign_model}}', ucwords($field->foreignKey->table), $replaceStub);
198
		$replaceStub = str_replace('{{field}}', $field->name, $replaceStub);
199
200
		if(isset($this->scaffolderConfig->generator->namespaces))
201
			$replaceStub = str_replace('{{model_namespace}}', $this->scaffolderConfig->generator->namespaces->models, $replaceStub);
202
203
		return $replaceStub;
204
		
205
	}
206
207
	/**
208
	 * Replace the fields strings by field.
209
	 *
210
	 * @param stdClass $field
211
	 * @param string $originalStubPart
212
	 *
213
	 * @return $this
214
	 */
215
	protected function replaceFieldStrings($field,  $originalStubPart){
216
		$replaceStub = str_replace('{{field}}', $field->name, $originalStubPart);
217
		
218
		if($this->eagerTable) $this->eagerTable.'.' ;
219
220
		$replaceStub = str_replace('{{eager_table}}', $this->eagerTable, $replaceStub);
221
222
		return $replaceStub;
223
		
224
	}
225
226
	/**
227
	 * Replace the prefix.
228
	 *
229
	 * @return $this
230
	 */
231
	protected function replaceRoutePrefix()
232
	{
233
		$this->stub = str_replace('{{route_prefix}}', $this->scaffolderConfig->generator->routing->prefix, $this->stub);
234
235
		return $this;
236
	}
237
238
	/**
239
	 * get anoter model data
240
	 *
241
	 * @param string $tableName
242
	 *
243
	 * @return $this
244
	 */
245
	protected $modelDataArray = [];
246
	protected function getModelData($tableName){
247
		
248
		if(array_key_exists($tableName, $this->modelDataArray)){
249
			return $this->modelDataArray[$tableName];
250
		}
251
		else {
252
253
			$modelFilename = base_path('scaffolder-config/models/') . $tableName . '.json' ;
254
255
			$modelData = Json::decodeFile($modelFilename);
256
257
			// Get model name
258
			$modelName = ucwords($tableName);
259
260
			// Get model hash
261
			$modelHash = md5_file($modelFilename);
262
263
			// Set model name
264
			$modelData->modelName = $modelName ;
265
266
			// Set model name
267
			$modelData->modelHash = $modelHash ;
268
269
			$this->modelDataArray[$tableName] = $modelData;
270
271
			return $this->modelDataArray[$tableName];
272
		}
273
	}
274
}