Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit 9a8d64c

Browse files
committed
implement support for lucid microservice installations
1 parent 70d0bb5 commit 9a8d64c

File tree

6 files changed

+62
-14
lines changed

6 files changed

+62
-14
lines changed

src/Commands/FeatureMakeCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function fire()
6767
'Feature class '.$feature->title.' created successfully.'.
6868
"\n".
6969
"\n".
70-
'Find it at <comment>'.strstr($feature->relativePath, 'src/').'</comment>'."\n"
70+
'Find it at <comment>'.$feature->relativePath.'</comment>'."\n"
7171
);
7272
} catch (Exception $e) {
7373
$this->error($e->getMessage());
@@ -82,8 +82,8 @@ public function fire()
8282
protected function getArguments()
8383
{
8484
return [
85-
['service', InputArgument::REQUIRED, 'The service in which the feature should be implemented.'],
8685
['feature', InputArgument::REQUIRED, 'The feature\'s name.'],
86+
['service', InputArgument::OPTIONAL, 'The service in which the feature should be implemented.'],
8787
];
8888
}
8989

src/Commands/JobMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public function fire()
7878
public function getArguments()
7979
{
8080
return [
81-
['domain', InputArgument::REQUIRED, 'The domain to be responsible for the job.'],
8281
['job', InputArgument::REQUIRED, 'The job\'s name.'],
82+
['domain', InputArgument::OPTIONAL, 'The domain to be responsible for the job.'],
8383
];
8484
}
8585

src/Components/Feature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class Feature extends Component
1818
{
19-
public function __construct($title, $file, $realPath, $relativePath, Service $service, $content = '')
19+
public function __construct($title, $file, $realPath, $relativePath, Service $service = null, $content = '')
2020
{
2121
$className = str_replace(' ', '', $title).'Feature';
2222

src/Finder.php

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Lucid\Console\Components\Job;
2121
use Symfony\Component\Finder\Finder as SymfonyFinder;
2222

23+
define('DS', DIRECTORY_SEPARATOR);
24+
2325
/**
2426
* @author Abed Halawi <[email protected]>
2527
*/
@@ -104,6 +106,31 @@ private function mapFuzzyResults($results)
104106
}, $results);
105107
}
106108

109+
/**
110+
* Get the source directory name.
111+
* In a microservice installation this will be `app`. `src` otherwise.
112+
*
113+
* @return string
114+
*/
115+
public function getSourceDirectoryName()
116+
{
117+
if (file_exists(base_path().'/'.$this->srcDirectoryName)) {
118+
return $this->srcDirectoryName;
119+
}
120+
121+
return 'app';
122+
}
123+
124+
/**
125+
* Determines whether this is a lucid microservice installation.
126+
*
127+
* @return bool
128+
*/
129+
public function isMicroservice()
130+
{
131+
return !($this->getSourceDirectoryName() === $this->srcDirectoryName);
132+
}
133+
107134
/**
108135
* Get the namespace used for the application.
109136
*
@@ -118,7 +145,7 @@ public function findRootNamespace()
118145

119146
// see which one refers to the "src/" directory
120147
foreach ($composer['autoload']['psr-4'] as $namespace => $directory) {
121-
if ($directory === $this->srcDirectoryName.'/') {
148+
if ($directory === $this->getSourceDirectoryName().'/') {
122149
return trim($namespace, '\\');
123150
}
124151
}
@@ -145,7 +172,9 @@ public function findFoundationNamespace()
145172
*/
146173
public function findServiceNamespace($service)
147174
{
148-
return $this->findRootNamespace().'\\Services\\'.$service;
175+
$root = $this->findRootNamespace();
176+
177+
return (!$service) ? $root : "$root\\Services\\$service";
149178
}
150179

151180
/**
@@ -155,7 +184,7 @@ public function findServiceNamespace($service)
155184
*/
156185
public function findSourceRoot()
157186
{
158-
return base_path().'/'.$this->srcDirectoryName;
187+
return ($this->isMicroservice()) ? app_path() : base_path().'/'.$this->srcDirectoryName;
159188
}
160189

161190
/**
@@ -170,14 +199,15 @@ public function findServicesRootPath()
170199

171200
/**
172201
* Find the path to the directory of the given service name.
202+
* In the case of a microservice service installation this will be app path.
173203
*
174204
* @param string $service
175205
*
176206
* @return string
177207
*/
178208
public function findServicePath($service)
179209
{
180-
return $this->findServicesRootPath()."/$service";
210+
return (!$service) ? app_path() : $this->findServicesRootPath()."/$service";
181211
}
182212

183213
/**
@@ -215,7 +245,9 @@ public function findFeaturePath($service, $feature)
215245
*/
216246
public function findFeatureTestPath($service, $test)
217247
{
218-
return $this->findServicePath($service)."/Tests/Features/$test.php";
248+
$root = ($service) ? $this->findServicePath($service).'/Tests' : base_path().'/tests';
249+
250+
return "$root/Features/$test.php";
219251
}
220252

221253
/**
@@ -347,7 +379,7 @@ public function listJobs($domainName = null)
347379
*/
348380
public function findJobPath($domain, $job)
349381
{
350-
return $this->findDomainPath($domain).'/Jobs/'.$job.'.php';
382+
return $this->findDomainPath($domain).DS.'Jobs'.DS.$job.'.php';
351383
}
352384

353385
/**
@@ -386,6 +418,22 @@ public function findDomainJobsTestsNamespace($domain)
386418
return $this->findDomainNamespace($domain).'\Tests\Jobs';
387419
}
388420

421+
/**
422+
* Get the path to the tests of the given domain.
423+
*
424+
* @param string $domain
425+
*
426+
* @return string
427+
*/
428+
public function findDomainTestsPath($domain)
429+
{
430+
if ($this->isMicroservice()) {
431+
return base_path().DS.'tests'.DS.'Domains'.DS.$domain;
432+
}
433+
434+
return $this->findDomainPath($domain).DS.'Tests';
435+
}
436+
389437
/**
390438
* Find the test path for the given job.
391439
*
@@ -396,7 +444,7 @@ public function findDomainJobsTestsNamespace($domain)
396444
*/
397445
public function findJobTestPath($domain, $jobTest)
398446
{
399-
return $this->findDomainPath($domain).DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.'Jobs'.DIRECTORY_SEPARATOR.$jobTest.'.php';
447+
return $this->findDomainTestsPath($domain).DS.'Jobs'.DS.$jobTest.'.php';
400448
}
401449

402450
/**
@@ -507,7 +555,7 @@ public function findFeature($name)
507555
$files = $finder->name($fileName)->in($this->findServicesRootPath())->files();
508556
foreach ($files as $file) {
509557
$path = $file->getRealPath();
510-
$serviceName = strstr($file->getRelativePath(), DIRECTORY_SEPARATOR, true);
558+
$serviceName = strstr($file->getRelativePath(), DS, true);
511559
$service = $this->findService($serviceName);
512560
$content = file_get_contents($path);
513561

src/Generators/FeatureGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function generate($feature, $service, array $jobs = [])
6666
basename($path),
6767
$path,
6868
$this->relativeFromReal($path),
69-
$this->findService($service),
69+
($service) ? $this->findService($service) : null,
7070
$content
7171
);
7272
}

src/Generators/JobGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private function generateTestFile($job, $domain)
9393
private function createDomainDirectory($domain)
9494
{
9595
$this->createDirectory($this->findDomainPath($domain).'/Jobs');
96-
$this->createDirectory($this->findDomainPath($domain).'/Tests/Jobs');
96+
$this->createDirectory($this->findDomainTestsPath($domain).'/Jobs');
9797
}
9898

9999
/**

0 commit comments

Comments
 (0)