@@ -32,8 +32,11 @@ public function __construct(BaseConfig $config, Schema $schema = null)
32
32
{
33
33
$ this ->config = $ config ;
34
34
35
- // Store an initial schema
36
- $ this ->schema = $ schema ?? new Schema ();
35
+ // Store initial schema
36
+ if (! is_null ($ schema ))
37
+ {
38
+ $ this ->schema = $ schema ;
39
+ }
37
40
}
38
41
39
42
/**
@@ -49,13 +52,78 @@ public function getErrors(): array
49
52
}
50
53
51
54
/**
52
- * Return the current schema
55
+ * Reset the current schema and errors
53
56
*
54
- * @return Schema The current schema object
57
+ * @return $this
58
+ */
59
+ public function reset ()
60
+ {
61
+ $ this ->schema = null ;
62
+ $ this ->errors = [];
63
+
64
+ return $ this ;
65
+ }
66
+
67
+ /**
68
+ * Set the current schema; used mostly for testing
69
+ *
70
+ * @return $this
71
+ */
72
+ public function setSchema (Schema $ schema )
73
+ {
74
+ $ this ->schema = $ schema ;
75
+
76
+ return $ this ;
77
+ }
78
+
79
+ /**
80
+ * Return the current schema; if automation is enabled then read or draft a missing schema
81
+ *
82
+ * @return Schema|null The current schema object
55
83
*/
56
84
public function get (): ?Schema
57
85
{
58
- return $ this ->schema ;
86
+ if (! is_null ($ this ->schema ))
87
+ {
88
+ return $ this ->schema ;
89
+ }
90
+
91
+ // No schema loaded - try the default reader
92
+ if ($ this ->config ->automate ['read ' ])
93
+ {
94
+ $ this ->read ();
95
+
96
+ if (! is_null ($ this ->schema ))
97
+ {
98
+ return $ this ->schema ;
99
+ }
100
+ }
101
+
102
+ // Still no schema - try a default draft
103
+ if ($ this ->config ->automate ['draft ' ])
104
+ {
105
+ $ this ->draft ();
106
+
107
+ if (! is_null ($ this ->schema ))
108
+ {
109
+ // If the draft succeeded check if we should archive it
110
+ if ($ this ->config ->automate ['archive ' ])
111
+ {
112
+ $ this ->archive ();
113
+ }
114
+
115
+ return $ this ->schema ;
116
+ }
117
+ }
118
+
119
+ // Absolute failure
120
+ if (! $ this ->config ->silent )
121
+ {
122
+ throw SchemasException::forNoSchema ();
123
+ }
124
+
125
+ $ this ->errors [] = lang ('Schemas.noSchema ' );
126
+ return null ;
59
127
}
60
128
61
129
/**
@@ -86,7 +154,14 @@ public function draft($handlers = null)
86
154
$ handler = new $ handler ($ this ->config );
87
155
}
88
156
89
- $ this ->schema ->merge ($ handler ->draft ());
157
+ if (is_null ($ this ->schema ))
158
+ {
159
+ $ this ->schema = $ handler ->draft ();
160
+ }
161
+ else
162
+ {
163
+ $ this ->schema ->merge ($ handler ->draft ());
164
+ }
90
165
91
166
$ this ->errors = array_merge ($ this ->errors , $ handler ->getErrors ());
92
167
}
@@ -153,8 +228,11 @@ public function read($handler = null)
153
228
154
229
$ this ->errors = array_merge ($ this ->errors , $ handler ->getErrors ());
155
230
156
- // Replace the current schema with a new one using the injected readHandler
157
- $ this ->schema = new Schema ($ handler );
231
+ // If all went well then set the current schema to a new one using the injected reader
232
+ if ($ handler ->ready ())
233
+ {
234
+ $ this ->schema = new Schema ($ handler );
235
+ }
158
236
159
237
return $ this ;
160
238
}
0 commit comments