Skip to content

Commit

Permalink
generate features in subdirectories
Browse files Browse the repository at this point in the history
this commit allows generating features in subdirectories when make:feature is called with a feature name having directory separators in between "/"
  • Loading branch information
Mulkave committed Oct 24, 2021
1 parent b041650 commit dc4460b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
14 changes: 14 additions & 0 deletions bin/test-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ lint "app/Features/TradeFeature.php"
examine "tests/Feature/TradeFeatureTest.php"
lint "tests/Feature/TradeFeatureTest.php"

## Feature in Subdirectory
./vendor/bin/lucid make:feature finance/wallet/pay
examine "app/Features/Finance/Wallet/PayFeature.php"
lint "app/Features/Finance/Wallet/PayFeature.php"
examine "tests/Feature/Finance/Wallet/PayFeatureTest.php"
lint "tests/Feature/Finance/Wallet/PayFeatureTest.php"

# Job
./vendor/bin/lucid make:job submitTradeRequest shipping
examine "app/Domains/Shipping/Jobs/SubmitTradeRequestJob.php"
Expand Down Expand Up @@ -111,6 +118,13 @@ lint "app/Services/Harbour/Features/TradeFeature.php"
examine "tests/Feature/Services/Harbour/TradeFeatureTest.php"
lint "tests/Feature/Services/Harbour/TradeFeatureTest.php"

## Feature in Subdirectory
./vendor/bin/lucid make:feature port/yacht/park harbour
examine "app/Services/Harbour/Features/Port/Yacht/ParkFeature.php"
lint "app/Services/Harbour/Features/Port/Yacht/ParkFeature.php"
examine "tests/Feature/Services/Harbour/Port/Yacht/ParkFeatureTest.php"
lint "tests/Feature/Services/Harbour/Port/Yacht/ParkFeatureTest.php"

## Operation
./vendor/bin/lucid make:operation spin harbour
examine "app/Services/Harbour/Operations/SpinOperation.php"
Expand Down
13 changes: 11 additions & 2 deletions src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,18 @@ public function findFeatureTestPath($service, $test)
* @return string
* @throws Exception
*/
public function findFeatureNamespace($service)
public function findFeatureNamespace($service, $feature)
{
return $this->findServiceNamespace($service).'\\Features';
$dirs = join('\\', explode(DS, dirname($feature)));

$base = $this->findServiceNamespace($service).'\\Features';

// greater than 1 because when there aren't subdirectories it will be "."
if (strlen($dirs) > 1) {
return $base.'\\'.$dirs;
}

return $base;
}

/**
Expand Down
21 changes: 15 additions & 6 deletions src/Generators/FeatureGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ public function generate($feature, $service, array $jobs = [])
$service = Str::service($service);

$path = $this->findFeaturePath($service, $feature);
$classname = $this->classname($feature);

if ($this->exists($path)) {
throw new Exception('Feature already exists!');

return false;
}

$namespace = $this->findFeatureNamespace($service);
$namespace = $this->findFeatureNamespace($service, $feature);

$content = file_get_contents($this->getStub());

Expand All @@ -40,7 +41,7 @@ public function generate($feature, $service, array $jobs = [])

$content = str_replace(
['{{feature}}', '{{namespace}}', '{{unit_namespace}}', '{{use_jobs}}', '{{run_jobs}}'],
[$feature, $namespace, $this->findUnitNamespace(), $useJobs, $runJobs],
[$classname, $namespace, $this->findUnitNamespace(), $useJobs, $runJobs],
$content
);

Expand All @@ -59,6 +60,13 @@ public function generate($feature, $service, array $jobs = [])
);
}

private function classname($feature)
{
$parts = explode(DS, $feature);

return array_pop($parts);
}

/**
* Generate the test file.
*
Expand All @@ -70,16 +78,17 @@ private function generateTestFile($feature, $service)
$content = file_get_contents($this->getTestStub());

$namespace = $this->findFeatureTestNamespace($service);
$featureNamespace = $this->findFeatureNamespace($service)."\\$feature";
$testClass = $feature.'Test';
$featureClass = $this->classname($feature);
$featureNamespace = $this->findFeatureNamespace($service, $feature)."\\".$featureClass;
$testClass = $featureClass.'Test';

$content = str_replace(
['{{namespace}}', '{{testclass}}', '{{feature}}', '{{feature_namespace}}'],
[$namespace, $testClass, Str::snake($feature), $featureNamespace],
[$namespace, $testClass, Str::snake(str_replace(DS, '', $feature)), $featureNamespace],
$content
);

$path = $this->findFeatureTestPath($service, $testClass);
$path = $this->findFeatureTestPath($service, $feature.'Test');

$this->createFile($path, $content);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ public static function realName($name, $pattern = '//')
*/
public static function feature($name)
{
return self::studly(preg_replace('/Feature(\.php)?$/', '', $name).'Feature');
$parts = array_map(function($part) { return self::studly($part); }, explode("/", $name));
$feature = self::studly(preg_replace('/Feature(\.php)?$/', '', array_pop($parts)).'Feature');

$parts[] = $feature;

return join(DS, $parts);
}

/**
Expand Down

0 comments on commit dc4460b

Please sign in to comment.