1111namespace Queryflatfile ;
1212
1313use Queryflatfile \TableBuilder ,
14- Queryflatfile \DriverInterface ;
14+ Queryflatfile \DriverInterface ,
15+ Queryflatfile \Exception \Query \TableNotFoundException ;
1516
1617/**
1718 * Pattern fluent pour la gestion d'un schéma de données.
@@ -47,6 +48,13 @@ class Schema
4748 * @var string
4849 */
4950 protected $ file ;
51+
52+ /**
53+ * Schéma des tables.
54+ *
55+ * @var array
56+ */
57+ protected $ schema = [];
5058
5159 /**
5260 * Construis l'objet avec une configuration.
@@ -74,12 +82,9 @@ public function __construct( $host = null, $name = 'schema',
7482 public function setConfig ( $ host , $ name = 'schema ' ,
7583 DriverInterface $ driver = null )
7684 {
77- $ this ->driver = $ driver ;
78- if ( is_null ($ driver ) )
79- {
80- $ this ->driver = new DriverJson ();
81- }
82-
85+ $ this ->driver = is_null ($ driver )
86+ ? new DriverJson ()
87+ : $ driver ;
8388 $ this ->path = $ host ;
8489 $ this ->name = $ name ;
8590 $ this ->file = $ host . DIRECTORY_SEPARATOR . $ name . '. ' . $ this ->driver ->getExtension ();
@@ -95,10 +100,17 @@ public function setConfig( $host, $name = 'schema',
95100 *
96101 * @return bool Si le schéma d'incrémentaion est bien enregistré.
97102 *
103+ * @throws TableNotFoundException
98104 */
99105 public function setIncrements ( $ table , array $ increments )
100106 {
101- $ schema = $ this ->getSchema ();
107+ $ schema = $ this ->getSchema ();
108+
109+ if ( !isset ($ schema [ $ table ]) )
110+ {
111+ throw new TableNotFoundException ("Table " . htmlspecialchars ($ table ) . " is not exist. " );
112+ }
113+
102114 $ schema [ $ table ][ 'increments ' ] = $ increments ;
103115
104116 return $ this ->save ($ this ->path , $ this ->name , $ schema );
@@ -111,14 +123,16 @@ public function setIncrements( $table, array $increments )
111123 */
112124 public function getSchema ()
113125 {
114- $ schema = $ this ->file ;
115-
116- if ( !file_exists ($ schema ) )
126+ if ( !file_exists ($ this ->file ) )
117127 {
118128 $ this ->create ($ this ->path , $ this ->name );
119129 }
130+ if ( empty ($ this ->schema ) )
131+ {
132+ $ this ->schema = $ this ->read ($ this ->path , $ this ->name );
133+ }
120134
121- return $ this ->read ( $ this -> path , $ this -> name ) ;
135+ return $ this ->schema ;
122136 }
123137
124138 /**
@@ -128,18 +142,16 @@ public function getSchema()
128142 *
129143 * @return array Schéma de la table.
130144 *
131- * @throws Exception\Query\ TableNotFoundException
145+ * @throws TableNotFoundException
132146 */
133147 public function getSchemaTable ( $ table )
134148 {
135- $ schema = $ this ->getSchema ();
136-
137- if ( !isset ($ schema [ $ table ]) )
149+ if ( !$ this ->hasTable ($ table ) )
138150 {
139- throw new Exception \ Query \ TableNotFoundException ("The " . $ table . " table is missing in the schema. " );
151+ throw new TableNotFoundException ("The " . htmlspecialchars ( $ table) . " table is missing in the schema. " );
140152 }
141153
142- return $ schema [ $ table ];
154+ return $ this -> getSchema () [ $ table ];
143155 }
144156
145157 /**
@@ -154,7 +166,7 @@ public function dropSchema()
154166 /* Supprime les fichiers des tables. */
155167 foreach ( $ schema as $ table )
156168 {
157- $ this ->delete ($ table [ ' path ' ] , $ table [ 'table ' ]);
169+ $ this ->delete ($ this -> path , $ table [ 'table ' ]);
158170 }
159171
160172 /* Supprime le fichier de schéma. */
@@ -180,34 +192,33 @@ public function dropSchema()
180192 * @param callable|null $callback fonction(TableBuilder $table) pour créer les champs.
181193 *
182194 * @return $this
195+ *
196+ * @throws \Exception
183197 */
184198 public function createTable ( $ table , callable $ callback = null )
185199 {
186200 $ schema = $ this ->getSchema ();
187201
188- if ( isset ( $ schema [ $ table ] ) )
202+ if ( $ this -> hasTable ( $ table ) )
189203 {
190204 throw new \Exception ("Table " . htmlspecialchars ($ table ) . " exist. " );
191205 }
192206
193- $ tableBuilder = null ;
194- $ increments = [];
207+ $ schema [ $ table ] = [
208+ 'table ' => $ table ,
209+ 'path ' => $ this ->path ,
210+ 'fields ' => null ,
211+ 'increments ' => []
212+ ];
195213
196214 if ( !is_null ($ callback ) )
197215 {
198- $ builder = new TableBuilder ();
216+ $ builder = new TableBuilder ();
199217 call_user_func_array ($ callback , [ &$ builder ]);
200- $ tableBuilder = $ builder ->build ();
201- $ increments = $ builder ->getIncrement ();
218+ $ schema [ $ table ][ ' fields ' ] = $ builder ->build ();
219+ $ schema [ $ table ][ ' increments ' ] = $ builder ->getIncrement ();
202220 }
203221
204- $ schema [ $ table ] = [
205- 'table ' => $ table ,
206- 'path ' => $ this ->path ,
207- 'fields ' => $ tableBuilder ,
208- 'increments ' => $ increments
209- ];
210-
211222 $ this ->save ($ this ->path , $ this ->name , $ schema );
212223 $ this ->create ($ this ->path , $ table );
213224
@@ -224,15 +235,12 @@ public function createTable( $table, callable $callback = null )
224235 */
225236 public function createTableIfNotExists ( $ table , callable $ callback = null )
226237 {
227- $ sch = $ this ->getSchema ();
228-
229- /* Créer la table si elle n'existe pas dans le schéma */
230- if ( !isset ($ sch [ $ table ]) )
238+ /* Créer la table si elle n'existe pas dans le schéma. */
239+ if ( !$ this ->hasTable ($ table ) )
231240 {
232241 $ this ->createTable ($ table , $ callback );
233- return $ this ;
234242 }
235- if ( !$ this ->driver ->has ($ sch [ $ table ][ ' path ' ] , $ sch [ $ table ][ ' table ' ] ) )
243+ else if ( !$ this ->driver ->has ($ this -> path , $ table ) )
236244 {
237245 /* Si elle existe dans le schéma et que le fichier est absent alors on le créer. */
238246 $ this ->create ($ this ->path , $ table );
@@ -266,7 +274,7 @@ public function hasTable( $table )
266274 {
267275 $ sch = $ this ->getSchema ();
268276
269- return isset ($ sch [ $ table ]) && $ this ->driver ->has ($ sch [ $ table ][ ' path ' ] , $ sch [ $ table ][ ' table ' ] );
277+ return isset ($ sch [ $ table ]) && $ this ->driver ->has ($ this -> path , $ table );
270278 }
271279
272280 /**
@@ -281,7 +289,7 @@ public function hasColumn( $table, $column )
281289 {
282290 $ sch = $ this ->getSchema ();
283291
284- return isset ($ sch [ $ table ][ $ column ]) && $ this ->driver ->has ($ sch [ $ table ][ ' path ' ] , $ sch [ $ table ][ ' table ' ] );
292+ return isset ($ sch [ $ table ][' fields ' ][ $ column ]) && $ this ->driver ->has ($ this -> path , $ table );
285293 }
286294
287295 /**
@@ -290,18 +298,18 @@ public function hasColumn( $table, $column )
290298 * @param String $table Nom de la table.
291299 *
292300 * @return bool
301+ *
302+ * @throws TableNotFoundException
293303 */
294304 public function truncateTable ( $ table )
295305 {
296- $ schema = $ this ->getSchema ();
297-
298- if ( !isset ($ schema [ $ table ]) )
306+ if ( !$ this ->hasTable ($ table ) )
299307 {
300- throw new \ Exception ("Table " . htmlspecialchars ($ table ) . " is not exist. " );
308+ throw new TableNotFoundException ("Table " . htmlspecialchars ($ table ) . " is not exist. " );
301309 }
302310
303- $ this ->save ($ schema [ $ table ][ ' path ' ] , $ schema [ $ table ][ ' table ' ] , [
304- ] );
311+ $ this ->save ($ this -> path , $ table , []);
312+ $ schema = $ this -> getSchema ( );
305313
306314 foreach ( $ schema [ $ table ][ 'increments ' ] as $ key => $ value )
307315 {
@@ -318,18 +326,19 @@ public function truncateTable( $table )
318326 *
319327 * @return bool Si la suppression du schema et des données se son bien passé.
320328 *
329+ * @throws TableNotFoundException
321330 */
322331 public function dropTable ( $ table )
323332 {
324333 $ schema = $ this ->getSchema ();
325334
326- if ( !isset ( $ schema [ $ table ] ) )
335+ if ( !$ this -> hasTable ( $ table ) )
327336 {
328- throw new \ Exception ("Table " . htmlspecialchars ($ table ) . " is not exist. " );
337+ throw new TableNotFoundException ("Table " . htmlspecialchars ($ table ) . " is not exist. " );
329338 }
330339
331- $ deleteSchema = $ this ->delete ($ schema [ $ table ][ 'path ' ], $ schema [ $ table ][ 'table ' ]);
332340 unset($ schema [ $ table ]);
341+ $ deleteSchema = $ this ->delete ($ this ->path , $ table );
333342 $ deleteData = $ this ->save ($ this ->path , $ this ->name , $ schema );
334343
335344 return $ deleteSchema && $ deleteData ;
@@ -344,10 +353,9 @@ public function dropTable( $table )
344353 */
345354 public function dropTableIfExists ( $ table )
346355 {
347- if ( $ this ->hasTable ($ table ) )
348- {
349- return $ this ->dropTable ($ table );
350- }
356+ return $ this ->hasTable ($ table )
357+ ? $ this ->dropTable ($ table )
358+ : false ;
351359 }
352360
353361 /**
@@ -384,7 +392,9 @@ public function read( $path, $file )
384392 */
385393 public function save ( $ path , $ file , array $ data )
386394 {
387- return $ this ->driver ->save ($ path , $ file , $ data );
395+ $ output = $ this ->driver ->save ($ path , $ file , $ data );
396+ $ this ->reloadSchema ();
397+ return $ output ;
388398 }
389399
390400 /**
@@ -398,7 +408,8 @@ public function save( $path, $file, array $data )
398408 */
399409 protected function create ( $ path , $ file , array $ data = [] )
400410 {
401- return $ this ->driver ->create ($ path , $ file , $ data );
411+ $ output = $ this ->driver ->create ($ path , $ file , $ data );
412+ return $ output ;
402413 }
403414
404415 /**
@@ -411,6 +422,13 @@ protected function create( $path, $file, array $data = [] )
411422 */
412423 protected function delete ( $ path , $ file )
413424 {
414- return $ this ->driver ->delete ($ path , $ file );
425+ $ output = $ this ->driver ->delete ($ path , $ file );
426+ $ this ->reloadSchema ();
427+ return $ output ;
428+ }
429+
430+ private function reloadSchema ()
431+ {
432+ $ this ->schema = $ this ->read ($ this ->path , $ this ->name );
415433 }
416434}
0 commit comments