JeffreyWay / Laravel-4-Generators

Rapidly speed up your Laravel workflow with generators
https://packagist.org/packages/way/generators
MIT License
27 stars 9 forks source link

generators do not respect php artisan app:name #422

Closed carcinocron closed 9 years ago

carcinocron commented 9 years ago

If someone names the namespace via php artisan app:name, generators continue to use the default one.

Luckyfox commented 9 years ago

Same for me

jonsa commented 9 years ago

This patch make use of Illuminate\Console\AppNamespaceDetectorTrait to resolve the app namespace and properly namespace models and controllers. I couldn't get the test environment working, since it doesn't seem to be fully ported to laravel 5, therefor the patch format. Perhaps @JeffreyWay could manually patch the library or a pull request can be made after the test environment is up and running.

Edit updated patch to include seeder

Index: src/Way/Generators/templates/Data/Controller.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/Way/Generators/templates/Data/Controller.php    (revision 457228dc2c0bef7ef75014f8e711def7732fb0ef)
+++ src/Way/Generators/templates/Data/Controller.php    (revision )
@@ -82,7 +82,7 @@
      */
     public function getNamespace()
     {
-        return 'App\Http\Controllers';
+        return 'Http\Controllers';
     }

 } 
\ No newline at end of file
Index: src/Way/Generators/templates/seed.txt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/Way/Generators/templates/seed.txt   (revision 457228dc2c0bef7ef75014f8e711def7732fb0ef)
+++ src/Way/Generators/templates/seed.txt   (revision )
@@ -12,7 +12,7 @@

        foreach(range(1, 10) as $index)
        {
-           $MODEL$::create([
+           $NAMESPACE$$MODEL$::create([

            ]);
        }
Index: src/Way/Generators/Commands/SeederGeneratorCommand.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/Way/Generators/Commands/SeederGeneratorCommand.php  (revision 457228dc2c0bef7ef75014f8e711def7732fb0ef)
+++ src/Way/Generators/Commands/SeederGeneratorCommand.php  (revision )
@@ -1,5 +1,6 @@
 <?php namespace Way\Generators\Commands;

+use Illuminate\Console\AppNamespaceDetectorTrait;
 use Illuminate\Support\Str;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputArgument;
@@ -7,6 +8,8 @@

 class SeederGeneratorCommand extends GeneratorCommand {

+    use AppNamespaceDetectorTrait;
+
     /**
      * The console command name.
      *
@@ -45,7 +48,8 @@

         return [
             'CLASS' => "{$tableName}TableSeeder",
-            'MODEL' => str_singular($tableName)
+            'MODEL' => str_singular($tableName),
+            'NAMESPACE' => $this->getAppNamespace()
         ];
     }

Index: src/Way/Generators/Commands/ModelGeneratorCommand.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/Way/Generators/Commands/ModelGeneratorCommand.php   (revision 457228dc2c0bef7ef75014f8e711def7732fb0ef)
+++ src/Way/Generators/Commands/ModelGeneratorCommand.php   (revision )
@@ -1,10 +1,13 @@
 <?php namespace Way\Generators\Commands;

+use Illuminate\Console\AppNamespaceDetectorTrait;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputArgument;

 class ModelGeneratorCommand extends GeneratorCommand {

+    use AppNamespaceDetectorTrait;
+
     /**
      * The console command name.
      *
@@ -40,7 +43,7 @@
     {
         return [
             'NAME' => ucwords($this->argument('modelName')),
-            'NAMESPACE' => 'App'
+            'NAMESPACE' => $this->getAppNamespace()
         ];
     }

Index: src/Way/Generators/Commands/ControllerGeneratorCommand.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/Way/Generators/Commands/ControllerGeneratorCommand.php  (revision 457228dc2c0bef7ef75014f8e711def7732fb0ef)
+++ src/Way/Generators/Commands/ControllerGeneratorCommand.php  (revision )
@@ -1,11 +1,14 @@
 <?php namespace Way\Generators\Commands;

+use Illuminate\Console\AppNamespaceDetectorTrait;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputArgument;
 use Way\Generators\Templates\Data\Controller as ControllerData;

 class ControllerGeneratorCommand extends GeneratorCommand {

+    use AppNamespaceDetectorTrait;
+
     /**
      * The console command name.
      *
@@ -37,7 +40,9 @@
      */
     protected function getTemplateData()
     {
-        return (new ControllerData($this->argument('controllerName')))->fetch();
+        $data = (new ControllerData($this->argument('controllerName')))->fetch();
+        $data['app_namespace'] = $this->getAppNamespace();
+        return $data;
     }

     /**
Index: src/Way/Generators/templates/controller.txt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/Way/Generators/templates/controller.txt (revision 457228dc2c0bef7ef75014f8e711def7732fb0ef)
+++ src/Way/Generators/templates/controller.txt (revision )
@@ -1,4 +1,4 @@
-<?php namespace $NAMESPACE$;
+<?php namespace $APP_NAMESPACE$$NAMESPACE$;

 use Illuminate\Routing\Controller;

\ No newline at end of file
JeffreyWay commented 9 years ago

Use https://github.com/laracasts/Laravel-5-Generators-Extended instead for L5.