44
55use ChrisPenny \DataObjectToFixture \Service \FixtureService ;
66use Exception ;
7- use SilverStripe \Control \HTTPRequest ;
87use SilverStripe \Core \ClassInfo ;
98use SilverStripe \Core \Config \Config ;
109use SilverStripe \Dev \BuildTask ;
10+ use SilverStripe \Model \List \PaginatedList ;
1111use SilverStripe \ORM \DataObject ;
12- use SilverStripe \ORM \PaginatedList ;
12+ use SilverStripe \PolyExecution \PolyOutput ;
13+ use Symfony \Component \Console \Command \Command ;
14+ use Symfony \Component \Console \Input \InputInterface ;
15+ use Symfony \Component \Console \Input \InputOption ;
1316use Throwable ;
1417
1518/**
1821class GenerateFixtureFromDataObject extends BuildTask
1922{
2023
21- protected $ title = 'Generate Fixture From DataObject ' ; // phpcs:ignore
24+ protected static string $ commandName = 'generate-fixture-from-dataobject ' ;
2225
23- protected $ description = 'Generate a text fixture from a DataObject in your Database ' ; // phpcs:ignore
26+ protected string $ title = 'Generate Fixture From DataObject ' ;
2427
25- private static string $ segment = 'generate- fixture- from-dataobject ' ; // phpcs:ignore
28+ protected static string $ description = 'Generate a text fixture from a DataObject in your Database ' ;
2629
2730 private ?int $ previousExecution = null ;
2831
29- /**
30- * @param HTTPRequest|mixed $request
31- * @throws Exception
32- */
33- public function run ($ request ): void // phpcs:ignore
32+ protected function execute (InputInterface $ input , PolyOutput $ output ): int
3433 {
3534 try {
3635 $ this ->previousExecution = ini_get ('max_execution_time ' );
3736 ini_set ('max_execution_time ' , 60 );
3837
3938 $ this ->outputStyles ();
4039
41- $ className = $ request -> getVar ('ClassName ' );
42- $ id = $ request -> getVar ('ID ' );
40+ $ className = $ input -> getOption ('ClassName ' );
41+ $ id = $ input -> getOption ('ID ' );
4342
4443 // We have both a ClassName and ID, which means we can render out the fixture for a particular record
4544 if ($ className && $ id ) {
46- $ this ->outputFixture ($ request , $ className , (int )$ id );
45+ $ this ->outputFixture ($ className , (int ) $ id );
4746
48- return ;
47+ return Command:: SUCCESS ;
4948 }
5049
5150 // We have a ClassName, but not ID yet, this means the user needs to be provided with a list of available
5251 // records for that particular class
5352 if ($ className ) {
54- $ this ->outputClassForm ($ request , $ className );
53+ $ this ->outputClassForm ($ input , $ className );
5554
56- return ;
55+ return Command:: SUCCESS ;
5756 }
5857
5958 // The initial form is a list of available classes
@@ -62,6 +61,8 @@ public function run($request): void // phpcs:ignore
6261 throw $ e ;
6362 } finally {
6463 ini_set ('max_execution_time ' , $ this ->previousExecution );
64+
65+ return Command::SUCCESS ;
6566 }
6667 }
6768
@@ -100,10 +101,10 @@ protected function outputInitialForm(): void
100101 echo '</form> ' ;
101102 }
102103
103- protected function outputClassForm (HTTPRequest $ request , string $ className ): void
104+ protected function outputClassForm (InputInterface $ input , string $ className ): void
104105 {
105106 $ dbFields = Config::inst ()->get ($ className , 'db ' );
106- $ start = $ request -> getVar ('start ' ) ?? 0 ;
107+ $ start = $ input -> getOption ('start ' ) ?? 0 ;
107108
108109 /** @var PaginatedList|DataObject[] $paginatedList */
109110 $ paginatedList = PaginatedList::create ($ className ::get ());
@@ -117,7 +118,10 @@ protected function outputClassForm(HTTPRequest $request, string $className): voi
117118 return ;
118119 }
119120
120- echo '<p><a href="/dev/tasks/generate-fixture-from-dataobject">< Back to the beginning</a></p> ' ;
121+ echo sprintf (
122+ '<p><a href="/dev/tasks/%s">< Back to the beginning</a></p> ' ,
123+ self ::$ commandName
124+ );
121125
122126 echo '<div class="pagination"> ' ;
123127 echo '<p><strong>Pagination:</strong></p> ' ;
@@ -149,7 +153,7 @@ protected function outputClassForm(HTTPRequest $request, string $className): voi
149153
150154 // Remove Datatime and Boolean fields, as they're (likely) not that useful for trying to find a specific record
151155 $ dbFields = array_diff ($ dbFields , ['Datetime ' , 'Boolean ' ]);
152- $ linkTemplate = '<td><a href="/dev/tasks/generate-fixture-from-dataobject ?ClassName=%s&ID=%s">Link</a></td> ' ;
156+ $ linkTemplate = '<td><a href="/dev/tasks/%s ?ClassName=%s&ID=%s">Link</a></td> ' ;
153157
154158 echo '<table> ' ;
155159 echo '<thead> ' ;
@@ -168,7 +172,7 @@ protected function outputClassForm(HTTPRequest $request, string $className): voi
168172
169173 foreach ($ paginatedList as $ dataObject ) {
170174 echo '<tr> ' ;
171- echo sprintf ($ linkTemplate , $ className , $ dataObject ->ID );
175+ echo sprintf ($ linkTemplate , self :: $ commandName , $ className , $ dataObject ->ID );
172176 echo sprintf ('<td>%s</td> ' , $ dataObject ->ID );
173177
174178 foreach (array_keys ($ dbFields ) as $ fieldName ) {
@@ -184,12 +188,9 @@ protected function outputClassForm(HTTPRequest $request, string $className): voi
184188 }
185189
186190 /**
187- * @param HTTPRequest $request
188- * @param string $className
189- * @param int $id
190191 * @throws Exception
191192 */
192- protected function outputFixture (HTTPRequest $ request , string $ className , int $ id ): void
193+ protected function outputFixture (string $ className , int $ id ): void
193194 {
194195 if (!$ className ) {
195196 echo '<p>No ClassName provided</p> ' ;
@@ -208,6 +209,11 @@ protected function outputFixture(HTTPRequest $request, string $className, int $i
208209
209210 if ($ dataObject === null ) {
210211 echo sprintf ('<p>DataObject not found. ClassName: %s, ID: %s</p> ' , $ className , $ id );
212+ echo sprintf (
213+ '<p><a href="/dev/tasks/%s?ClassName=%s">< Back to list of records</a></p> ' ,
214+ self ::$ commandName ,
215+ $ className
216+ );
211217
212218 return ;
213219 }
@@ -220,7 +226,8 @@ protected function outputFixture(HTTPRequest $request, string $className, int $i
220226 }
221227
222228 echo sprintf (
223- '<p><a href="/dev/tasks/generate-fixture-from-dataobject?ClassName=%s">< Back to list of records</a></p> ' ,
229+ '<p><a href="/dev/tasks/%s?ClassName=%s">< Back to list of records</a></p> ' ,
230+ self ::$ commandName ,
224231 $ className
225232 );
226233
@@ -348,4 +355,28 @@ protected function outputStyles(): void
348355 echo '</style> ' ;
349356 }
350357
358+ public function getOptions (): array
359+ {
360+ return [
361+ new InputOption (
362+ 'ClassName ' ,
363+ null ,
364+ InputOption::VALUE_OPTIONAL ,
365+ 'Specify class of the DataObject to be exported '
366+ ),
367+ new InputOption (
368+ 'ID ' ,
369+ null ,
370+ InputOption::VALUE_OPTIONAL ,
371+ 'Specify the ID of the DataObject to be exported '
372+ ),
373+ new InputOption (
374+ 'start ' ,
375+ null ,
376+ InputOption::VALUE_OPTIONAL ,
377+ 'Pagination value '
378+ ),
379+ ];
380+ }
381+
351382}
0 commit comments