Skip to content

Commit ae63c20

Browse files
committed
Clean-ups
Left overs to clean and changes from discussion. Fixed config path finding
1 parent 712a89b commit ae63c20

10 files changed

+102
-117
lines changed

bin/daenerys

-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@ includeIfExists(getcwd() . '/vendor/autoload.php') ||
1616
includeIfExists(__DIR__ . '/../vendor/autoload.php') ||
1717
includeIfExists(__DIR__ . '/../autoload.php');
1818

19-
putenv("LOTGD_CONFIG=./config/test.yml");
20-
2119
$daenerys = new DaenerysConsole();
2220
$daenerys->run();

config/lotgd.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
database:
2+
dsn: "sqlite::memory:"
3+
name: daenerys
4+
user: root
5+
password:
6+
game:
7+
epoch: 2016-07-01 00:00:00.0 -8
8+
offsetSeconds: 0
9+
daysPerDay: 1
10+
logs:
11+
path: ../logs

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<phpunit bootstrap="bootstrap/bootstrap.php">
22
<php>
3-
<env name="LOTGD_CONFIG" value="./config/test.yml" />
3+
<env name="LOTGD_CONFIG" value="/config/test.yml" />
44
</php>
55
<testsuites>
66
<testsuite name="All">

src/BootConfiguration.php

+45-12
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,33 @@ class BootConfiguration
2424
private $rawConfig;
2525
private $models;
2626
private $daenerysCommands;
27+
private $cwd;
2728

28-
public function __construct(ComposerManager $composerManager, PackageInterface $package)
29+
public function __construct(ComposerManager $composerManager, PackageInterface $package, string $cwd)
2930
{
3031
$this->composerManager = $composerManager;
3132
$this->package = $package;
33+
$this->cwd = $cwd;
3234

3335
$installationManager = $composerManager->getComposer()->getInstallationManager();
34-
$confFile = $installationManager->getInstallPath($package) . "/lotgd.yml";
36+
37+
// only lotgd-modules are installed in the vendor directory
38+
if ($package->getType() === "lotgd-module") {
39+
$confFile = $installationManager->getInstallPath($package) . "/lotgd.yml";
40+
}
41+
else {
42+
$confFile = $cwd . "/lotgd.yml";
43+
}
3544

3645
$this->rootNamespace = $this->findRootNamespace($package);
37-
$this->rawConfig = Yaml::parse(file_get_contents($confFile));
46+
if (file_exists($confFile)) {
47+
$this->rawConfig = Yaml::parse(file_get_contents($confFile));
48+
}
49+
else {
50+
$name = $package->getName();
51+
$type = $package->getType();
52+
throw new \Exception("Package {$name} of type {$type} does not have a lotgd.yml in it's root ($confFile).");
53+
}
3854

3955
$this->findEntityDirectory();
4056
$this->findDenerysCommands();
@@ -45,7 +61,6 @@ public function __construct(ComposerManager $composerManager, PackageInterface $
4561
*
4662
* This function searches the package's configuration to find it's root namespace.
4763
* For this, it uses the following order:
48-
* - look in ["extra"]["lotgd-namespace"]
4964
* - check psr-4 autoload configuration. If used, it takes the first element
5065
* - check psr-0 autoload configuration. If used, it takes the first element
5166
* @param PackageInterface $package
@@ -54,24 +69,24 @@ public function __construct(ComposerManager $composerManager, PackageInterface $
5469
*/
5570
protected function findRootNamespace(PackageInterface $package): string
5671
{
57-
// if one is defined, we use that.
58-
if (isset($package->getExtra()["lotgd-namespace"])) {
59-
return $package->getExtra()["lotgd-namespace"];
60-
}
61-
6272
$autoload = $package->getAutoload();
6373
if (isset($autoload["psr-4"]) && count($autoload["psr-4"]) > 0) {
64-
return $autoload["psr-4"][0];
74+
return key($autoload["psr-4"]);
6575
}
6676

6777
if (isset($autoload["psr-0"]) && count($autoload["psr-0"]) > 0) {
68-
return $autoload["psr-0"][0];
78+
return key($autoload["psr-0"]);
6979
}
7080

7181
$name = $package->getName();
7282
throw new \Exception("{$name} has no valid namespace.");
7383
}
7484

85+
/**
86+
* Returns a subkey if it exists or null.
87+
* @param array $arguments
88+
* @return type
89+
*/
7590
protected function getSubKeyIfItExists(array $arguments)
7691
{
7792
$parent = $this->rawConfig;
@@ -125,7 +140,7 @@ protected function findEntityDirectory()
125140
$entityNamespace = $this->rootNamespace . $entityNamespace;
126141

127142
if (is_null($entityNamespace) === false) {
128-
$entityDirectory = $this->composerManager->getComposer()->translateNamespaceToPath($entityNamespace);
143+
$entityDirectory = $this->composerManager->translateNamespaceToPath($entityNamespace, $this->cwd);
129144

130145
if (is_dir($entityDirectory) === false) {
131146
throw new \Exception("{$entityDirectory}, generated from {$entityNamespace}, is not a valid directory.");
@@ -153,6 +168,10 @@ public function getEntityDirectory(): string
153168
return $this->entityDirectory;
154169
}
155170

171+
/**
172+
* Searches the config file for daenerys commands and, if found, adds the class name to a list
173+
* @return type
174+
*/
156175
protected function findDenerysCommands()
157176
{
158177
$list = $this->iterateKey("bootstrap", "daenerysCommands");
@@ -167,6 +186,20 @@ protected function findDenerysCommands()
167186
}
168187
}
169188

189+
/**
190+
* Returns true if this configuration has daenerys commands
191+
* @return bool
192+
*/
193+
public function hasDaenerysCommands(): bool
194+
{
195+
return count($this->daenerysCommands) > 0 ? true : false;
196+
}
197+
198+
/**
199+
* Adds daenerys commands from this configuration to the application.
200+
* @param \LotGD\Core\Game $game
201+
* @param Application $application
202+
*/
170203
public function addDaenerysCommands(Game $game, Application $application)
171204
{
172205
foreach ($this->daenerysCommands as $command) {

src/BootConfigurationManager.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(ComposerManager $composerManager, string $cwd)
2222
foreach($packages as $package)
2323
{
2424
if ($package->getType() === "lotgd-crate" || $package->getType() === "lotgd-module") {
25-
$this->configurations[] = new BootConfiguration($composerManager, $package);
25+
$this->configurations[] = new BootConfiguration($composerManager, $package, $cwd);
2626
}
2727
}
2828
}
@@ -48,7 +48,7 @@ public function addDaenerysCommands(Game $game, Application $application)
4848
{
4949
foreach ($this->configurations as $config) {
5050
if ($config->hasDaenerysCommands()) {
51-
$this->addDaenerysCommands($game, $application);
51+
$config->addDaenerysCommands($game, $application);
5252
}
5353
}
5454
}

src/Bootstrap.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ class Bootstrap
3232

3333
/**
3434
* Create a new Game object, with all the necessary configuration.
35+
* @param string $rootDir The root directory if it is different from getcwd()
3536
* @return Game The newly created Game object.
3637
*/
37-
public static function createGame(): Game
38+
public static function createGame(string $rootDir = null): Game
3839
{
3940
$game = new self();
40-
return $game->getGame();
41+
return $game->getGame($rootDir);
4142
}
4243

4344
/**
4445
* Starts the game kernel with the most important classes and returns the object
46+
* @param string $rootDir The root directory if it is different from getcwd()
4547
* @return Game
4648
*/
4749
public function getGame(string $rootDir = null): Game
@@ -64,7 +66,13 @@ public function getGame(string $rootDir = null): Game
6466
return $this->game;
6567
}
6668

67-
public function createBootConfigurationManager(
69+
/**
70+
* Creates the boot configuration manager
71+
* @param ComposerManager $composerManager
72+
* @param string $cwd
73+
* @return \LotGD\Core\BootConfigurationManager
74+
*/
75+
protected function createBootConfigurationManager(
6876
ComposerManager $composerManager,
6977
string $cwd
7078
): BootConfigurationManager {
@@ -136,7 +144,14 @@ protected function initPackageBootstraps(ComposerManager $composer): array
136144
*/
137145
protected function createConfiguration(): Configuration
138146
{
139-
$configFilePath = getenv('LOTGD_CONFIG') ?? "/config/lotgd.yml";
147+
$configFilePath = getenv('LOTGD_CONFIG');
148+
149+
if (empty($configFilePath)) {
150+
$configFilePath = $this->rootDir . "/config/lotgd.yml";
151+
}
152+
else {
153+
$configFilePath = $this->rootDir . $configFilePath;
154+
}
140155

141156
if ($configFilePath === false || strlen($configFilePath) == 0 || is_file($configFilePath) === false) {
142157
throw new InvalidConfigurationException("Invalid or missing configuration file: {$configFilePath}.");

src/ComposerManager.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ public function getModulePackages(): array
9797
/**
9898
* Find the filesystem path where the code for a namespace can be found.
9999
* @param string $namespace The namespace to translate.
100+
* @param string $cwd Current working directory
100101
* @return string|null Path representing $namespace or null if $namespace
101102
* cannot be found or if the path does not exist.
102103
*/
103-
public function translateNamespaceToPath(string $namespace)
104+
public function translateNamespaceToPath(string $namespace, string $cwd = null)
104105
{
105106
// Find the directory for this namespace by using the autoloader's
106107
// classmap.
107-
$autoloader = require(ComposerManager::findAutoloader());
108+
$cwd = $cwd ?? getcwd();
109+
$autoloader = require(ComposerManager::findAutoloader($cwd));
108110
$prefixes = $autoloader->getPrefixesPsr4();
109111

110112
// Standardize the namespace to remove any leading \ and add a trailing \
@@ -145,16 +147,18 @@ public function translateNamespaceToPath(string $namespace)
145147
/**
146148
* Returns a path (could be relative) to the proper autoload.php file in
147149
* the current setup.
150+
* @param string $cwd current working directory
148151
*/
149-
public static function findAutoloader(): string
152+
public static function findAutoloader(string $cwd): string
150153
{
151154
// Dance to find the autoloader.
152155
// TOOD: change this to open up the Composer config and use $c['config']['vendor-dir'] instead of "vendor"
153156
$order = [
154-
getcwd() . '/vendor/autoload.php',
157+
$cwd . '/vendor/autoload.php',
155158
__DIR__ . '/../vendor/autoload.php',
156159
__DIR__ . '/../autoload.php',
157160
];
161+
158162
foreach ($order as $path) {
159163
if (file_exists($path)) {
160164
return $path;

tests/BootstrapTest.php

+14-65
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,6 @@ public function testGame()
3232
$this->assertNotNull($g->getLogger());
3333
}
3434

35-
public function testWorkingFromChildWorkingDirectory()
36-
{
37-
$cwd = getcwd();
38-
$oldconf = getenv("LOTGD_CONFIG");
39-
chdir($cwd . "/tests/");
40-
putenv("LOTGD_CONFIG=../".$oldconf);
41-
42-
$this->assertStringEndsWith("/tests", getcwd());
43-
$this->assertStringStartsWith(".././", getenv("LOTGD_CONFIG"));
44-
45-
$game = Bootstrap::createGame();
46-
47-
chdir($cwd);
48-
putenv("LOTGD_CONFIG=" . $oldconf);
49-
50-
$this->assertStringEndsNotWith("/tests", getcwd());
51-
$this->assertStringStartsNotWith(".././", getenv("LOTGD_CONFIG"));
52-
}
53-
5435
public function testBootstrapLoadsPackageModels()
5536
{
5637
$installationManager = $this->getMockBuilder(InstallationManager::class)
@@ -61,27 +42,31 @@ public function testBootstrapLoadsPackageModels()
6142

6243
$composer = $this->getMockBuilder(\Composer\Composer::class)
6344
->disableOriginalConstructor()
64-
->setMethods(["getInstallationManager", "translateNamespaceToPath"])
45+
->setMethods(["getInstallationManager"])
6546
->getMock();
6647
$composer->method("getInstallationManager")->willReturn($installationManager);
67-
$composer
68-
->expects($this->exactly(1))
69-
->method("translateNamespaceToPath")
70-
->with("LotGD\\Core\\Tests\\FakeModule\\Models")
71-
->willReturn(__DIR__ . "/FakeModule/Models");
7248

7349
$fakeModulePackage = $this->getMockBuilder(AliasPackage::class)
7450
->disableOriginalConstructor()
75-
->setMethods(["getType", "getExtra"])
51+
->setMethods(["getType", "getAutoload"])
7652
->getMock();
7753
$fakeModulePackage->method("getType")->willReturn("lotgd-module");
78-
$fakeModulePackage->method("getExtra")->willReturn(["lotgd-namespace" => "LotGD\\Core\\Tests\\FakeModule\\"]);
54+
$fakeModulePackage->method("getAutoload")->willReturn([
55+
"psr-4" => [
56+
"LotGD\\Core\\Tests\\FakeModule\\" => "FakeModule/"
57+
]
58+
]);
7959

8060
$composerManager = $this->getMockBuilder(ComposerManager::class)
81-
->setMethods(["getPackages", "getComposer"])
61+
->setMethods(["getPackages", "getComposer", "translateNamespaceToPath"])
8262
->getMock();
8363
$composerManager->method("getPackages")->willReturn([$fakeModulePackage]);
8464
$composerManager->method("getComposer")->willReturn($composer);
65+
$composerManager
66+
->expects($this->exactly(1))
67+
->method("translateNamespaceToPath")
68+
->with("LotGD\\Core\\Tests\\FakeModule\\Models")
69+
->willReturn(__DIR__ . "/FakeModule/Models");
8570

8671
$bootstrap = $this->getMockBuilder(Bootstrap::class)
8772
->setMethods(["createComposerManager"])
@@ -93,42 +78,6 @@ public function testBootstrapLoadsPackageModels()
9378

9479
$this->assertGreaterThanorEqual(3, $bootstrap->getReadAnnotationDirectories());
9580

96-
/*$user = new UserEntity();
97-
$user->setName("Monthy");
98-
$game->getEntityManager()->persist($user);
99-
$game->getEntityManager()->flush();
100-
$id = $user->getId();
101-
$this->assertInternalType("int", $id);
102-
$game->getEntityManager()->clear();
103-
$user = $game->getEntityManager()->getRepository(UserEntity::class)->find($id);
104-
$this->assertInternalType("int", $user->getId());
105-
$this->assertInternalType("string", $user->getName());
106-
$this->assertSame("Monthy", $user->getName());*/
107-
}
108-
109-
/*public function testGenerateAnnotationDirectories()
110-
{
111-
$composerManager = $this->getMockBuilder(ComposerManager::class)
112-
->disableOriginalConstructor()
113-
->getMock();
114-
115-
$package = $this->getMockForAbstractClass(PackageInterface::class);
116-
$package->method('getName')->willReturn('lotgd/BootstrapTest');
117-
$package->method('getExtra')->willReturn(array(
118-
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
119-
));
120-
$composerManager->method('getPackages')->willReturn(array($package));
121-
122-
$bootstrap = $this->getMockBuilder(Bootstrap::class)
123-
->setMethods(["createComposerManager"])
124-
->getMock();
125-
126-
$bootstrap->method("createComposerManager")->willReturn($composerManager);
127-
128-
$game = $bootstrap->getGame();
129-
130-
$this->assertGreaterThanOrEqual(2, $bootstrap->getReadAnnotationDirectories());
131-
13281
$user = new UserEntity();
13382
$user->setName("Monthy");
13483
$game->getEntityManager()->persist($user);
@@ -140,5 +89,5 @@ public function testBootstrapLoadsPackageModels()
14089
$this->assertInternalType("int", $user->getId());
14190
$this->assertInternalType("string", $user->getName());
14291
$this->assertSame("Monthy", $user->getName());
143-
}*/
92+
}
14493
}

0 commit comments

Comments
 (0)