Skip to content

Commit e71f47c

Browse files
committed
Simplify factory config + use more lazy loading. Closes #117
1 parent e078eab commit e71f47c

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

app/config.php

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use PhpSchool\PhpWorkshop\Output\StdOutput;
3232
use PhpSchool\PhpWorkshop\Patch;
3333
use PhpSchool\PhpWorkshop\ResultAggregator;
34+
use PhpSchool\PSX\Factory as PsxFactory;
3435
use PhpSchool\PSX\SyntaxHighlighter;
3536
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
3637
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck;
@@ -57,7 +58,7 @@
5758

5859
return [
5960
'appName' => basename($_SERVER['argv'][0]),
60-
ExerciseDispatcher::class => factory(function (ContainerInterface $c) {
61+
ExerciseDispatcher::class => function (ContainerInterface $c) {
6162
$dispatcher = new ExerciseDispatcher(
6263
$c->get(RunnerFactory::class),
6364
$c->get(ResultAggregator::class),
@@ -70,9 +71,9 @@
7071
$dispatcher->requireCheck(PhpLintCheck::class);
7172
$dispatcher->requireCheck(CodeParseCheck::class);
7273
return $dispatcher;
73-
}),
74+
},
7475
ResultAggregator::class => object(ResultAggregator::class),
75-
CheckRepository::class => factory(function (ContainerInterface $c) {
76+
CheckRepository::class => function (ContainerInterface $c) {
7677
return new CheckRepository([
7778
$c->get(FileExistsCheck::class),
7879
$c->get(PhpLintCheck::class),
@@ -81,8 +82,8 @@
8182
$c->get(FunctionRequirementsCheck::class),
8283
$c->get(DatabaseCheck::class),
8384
]);
84-
}),
85-
CommandRouter::class => factory(function (ContainerInterface $c) {
85+
},
86+
CommandRouter::class => function (ContainerInterface $c) {
8687
return new CommandRouter(
8788
[
8889
new CommandDefinition('menu', [], MenuCommand::class),
@@ -95,48 +96,47 @@
9596
'menu',
9697
$c
9798
);
98-
}),
99+
},
99100

100-
Color::class => factory(function (ContainerInterface $c) {
101+
Color::class => function () {
101102
$colors = new Color;
102103
$colors->setForceStyle(true);
103104
return $colors;
104-
}),
105-
OutputInterface::class => factory(function (ContainerInterface $c) {
105+
},
106+
OutputInterface::class => function (ContainerInterface $c) {
106107
return new StdOutput($c->get(Color::class), $c->get(TerminalInterface::class));
107-
}),
108+
},
108109

109-
ExerciseRepository::class => factory(function (ContainerInterface $c) {
110+
ExerciseRepository::class => function (ContainerInterface $c) {
110111
return new ExerciseRepository(
111112
array_map(function ($exerciseClass) use ($c) {
112113
return $c->get($exerciseClass);
113114
}, $c->get('exercises'))
114115
);
115-
}),
116+
},
116117

117-
EventDispatcher::class => factory([new EventDispatcherFactory, '__invoke']),
118+
EventDispatcher::class => factory(EventDispatcherFactory::class),
119+
EventDispatcherFactory::class => object(),
118120

119121
//Exercise Runners
120-
RunnerFactory::class => factory(function (ContainerInterface $c) {
121-
return new RunnerFactory;
122-
}),
122+
RunnerFactory::class => object(),
123123

124124
//commands
125-
MenuCommand::class => factory(function (ContainerInterface $c) {
125+
MenuCommand::class => function (ContainerInterface $c) {
126126
return new MenuCommand($c->get('menu'));
127-
}),
127+
},
128128

129-
PrintCommand::class => factory(function (ContainerInterface $c) {
129+
PrintCommand::class => function (ContainerInterface $c) {
130130
return new PrintCommand(
131131
$c->get('appName'),
132132
$c->get(ExerciseRepository::class),
133133
$c->get(UserState::class),
134134
$c->get(MarkdownRenderer::class),
135135
$c->get(OutputInterface::class)
136136
);
137-
}),
137+
},
138138

139-
VerifyCommand::class => factory(function (ContainerInterface $c) {
139+
VerifyCommand::class => function (ContainerInterface $c) {
140140
return new VerifyCommand(
141141
$c->get(ExerciseRepository::class),
142142
$c->get(ExerciseDispatcher::class),
@@ -145,77 +145,78 @@
145145
$c->get(OutputInterface::class),
146146
$c->get(ResultsRenderer::class)
147147
);
148-
}),
148+
},
149149

150-
RunCommand::class => factory(function (ContainerInterface $c) {
150+
RunCommand::class => function (ContainerInterface $c) {
151151
return new RunCommand(
152152
$c->get(ExerciseRepository::class),
153153
$c->get(ExerciseDispatcher::class),
154154
$c->get(UserState::class),
155155
$c->get(UserStateSerializer::class),
156156
$c->get(OutputInterface::class)
157157
);
158-
}),
158+
},
159159

160-
CreditsCommand::class => factory(function (ContainerInterface $c) {
160+
CreditsCommand::class => function (ContainerInterface $c) {
161161
return new CreditsCommand(
162162
$c->get('coreContributors'),
163163
$c->get('appContributors'),
164164
$c->get(OutputInterface::class),
165165
$c->get(Color::class)
166166
);
167-
}),
167+
},
168168

169-
HelpCommand::class => factory(function (ContainerInterface $c) {
169+
HelpCommand::class => function (ContainerInterface $c) {
170170
return new HelpCommand(
171171
$c->get('appName'),
172172
$c->get(OutputInterface::class),
173173
$c->get(Color::class)
174174
);
175-
}),
175+
},
176176

177177
//Listeners
178178
PrepareSolutionListener::class => object(),
179-
CodePatchListener::class => factory(function (ContainerInterface $c) {
179+
CodePatchListener::class => function (ContainerInterface $c) {
180180
return new CodePatchListener($c->get(CodePatcher::class));
181-
}),
182-
SelfCheckListener::class => factory(function (ContainerInterface $c) {
181+
},
182+
SelfCheckListener::class => function (ContainerInterface $c) {
183183
return new SelfCheckListener($c->get(ResultAggregator::class));
184-
}),
184+
},
185185

186186
//checks
187-
FileExistsCheck::class => object(FileExistsCheck::class),
188-
PhpLintCheck::class => object(PhpLintCheck::class),
189-
CodeParseCheck::class => factory(function (ContainerInterface $c) {
187+
FileExistsCheck::class => object(),
188+
PhpLintCheck::class => object(),
189+
CodeParseCheck::class => function (ContainerInterface $c) {
190190
return new CodeParseCheck($c->get(Parser::class));
191-
}),
192-
FunctionRequirementsCheck::class => factory(function (ContainerInterface $c) {
191+
},
192+
FunctionRequirementsCheck::class => function (ContainerInterface $c) {
193193
return new FunctionRequirementsCheck($c->get(Parser::class));
194-
}),
195-
DatabaseCheck::class => object(DatabaseCheck::class),
196-
ComposerCheck::class => object(ComposerCheck::class),
194+
},
195+
DatabaseCheck::class => object(),
196+
ComposerCheck::class => object(),
197197

198198
//Utils
199-
Filesystem::class => object(Filesystem::class),
200-
Parser::class => factory(function (ContainerInterface $c) {
199+
Filesystem::class => object(),
200+
Parser::class => function () {
201201
$parserFactory = new ParserFactory;
202202
return $parserFactory->create(ParserFactory::PREFER_PHP7);
203-
}),
204-
CodePatcher::class => factory(function (ContainerInterface $c) {
203+
},
204+
CodePatcher::class => function (ContainerInterface $c) {
205205
$patch = (new Patch)
206206
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'ini_set("display_errors", 1);'))
207207
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'error_reporting(E_ALL);'))
208208
->withInsertion(new Insertion(Insertion ::TYPE_BEFORE, 'date_default_timezone_set("Europe/London");'));
209209

210210
return new CodePatcher($c->get(Parser::class), new Standard, $patch);
211-
}),
212-
FakerGenerator::class => factory(function (ContainerInterface $c) {
211+
},
212+
FakerGenerator::class => function () {
213213
return FakerFactory::create();
214-
}),
214+
},
215215

216216
TerminalInterface::class => factory([TerminalFactory::class, 'fromSystem']),
217-
'menu' => factory([new MenuFactory, '__invoke']),
218-
ExerciseRenderer::class => factory(function (ContainerInterface $c) {
217+
'menu' => factory(MenuFactory::class),
218+
MenuFactory::class => object(),
219+
ExerciseRenderer::class => function (ContainerInterface $c) {
219220
return new ExerciseRenderer(
220221
$c->get('appName'),
221222
$c->get(ExerciseRepository::class),
@@ -225,33 +226,32 @@
225226
$c->get(Color::class),
226227
$c->get(OutputInterface::class)
227228
);
228-
}),
229-
MarkdownRenderer::class => factory(function (ContainerInterface $c) {
229+
},
230+
MarkdownRenderer::class => function (ContainerInterface $c) {
230231
$docParser = new DocParser(Environment::createCommonMarkEnvironment());
231232
$cliRenderer = (new MarkdownCliRendererFactory)->__invoke($c);
232233
return new MarkdownRenderer($docParser, $cliRenderer);
233-
}),
234-
UserStateSerializer::class => factory(function (ContainerInterface $c) {
234+
},
235+
UserStateSerializer::class => function (ContainerInterface $c) {
235236
return new UserStateSerializer(
236237
getenv('HOME'),
237238
$c->get('workshopTitle'),
238239
$c->get(ExerciseRepository::class)
239240
);
240-
}),
241-
UserState::class => factory(function (ContainerInterface $c) {
241+
},
242+
UserState::class => function (ContainerInterface $c) {
242243
return $c->get(UserStateSerializer::class)->deSerialize();
243-
}),
244-
SyntaxHighlighter::class => factory(function (ContainerInterface $c) {
245-
return (new \PhpSchool\PSX\Factory)->__invoke();
246-
}),
247-
ResetProgress::class => factory(function (ContainerInterface $c) {
244+
},
245+
SyntaxHighlighter::class => factory(PsxFactory::class),
246+
PsxFactory::class => object(),
247+
ResetProgress::class => function (ContainerInterface $c) {
248248
return new ResetProgress(
249249
$c->get(UserStateSerializer::class),
250250
$c->get(OutputInterface::class)
251251
);
252-
}),
252+
},
253253
ResultRendererFactory::class => object(),
254-
ResultsRenderer::class => factory(function (ContainerInterface $c) {
254+
ResultsRenderer::class => function (ContainerInterface $c) {
255255
return new ResultsRenderer(
256256
$c->get('appName'),
257257
$c->get(Color::class),
@@ -260,7 +260,7 @@
260260
$c->get(SyntaxHighlighter::class),
261261
$c->get(ResultRendererFactory::class)
262262
);
263-
}),
263+
},
264264
'coreContributors' => [
265265
'@AydinHassan' => 'Aydin Hassan',
266266
'@mikeymike' => 'Michael Woodward',

0 commit comments

Comments
 (0)