Permalink
Browse files

Refactor code

  • Loading branch information...
1 parent 0daf2fb commit 3ce76c585ec725067a3219cd004874f7b9da281c @JackieDo committed Jan 11, 2017
Showing with 192 additions and 34 deletions.
  1. +93 −8 PackageCreator.php
  2. +1 −1 stubs/config.stub
  3. +20 −0 stubs/controller.stub
  4. +22 −0 stubs/facades.stub
  5. +14 −0 stubs/main.stub
  6. +6 −0 stubs/plain.provider.stub
  7. +35 −22 stubs/provider.stub
  8. +1 −3 stubs/routes.stub
View
@@ -30,9 +30,9 @@ class PackageCreator {
protected $blocks = array(
'SupportFiles',
'SupportDirectories',
- 'PublicDirectory',
'TestDirectory',
'ServiceProvider',
+ 'SourceClassDirectories'
);
/**
@@ -188,7 +188,15 @@ public function writeIgnoreFile(Package $package, $directory, $plain)
*/
public function writeSupportDirectories(Package $package, $directory)
{
- foreach (array('config', 'controllers', 'lang', 'migrations', 'views', 'assets', 'routes') as $support)
+ $supports = array(
+ 'config',
+ 'migrations',
+ 'resources/assets',
+ 'resources/lang',
+ 'resources/views'
+ );
+
+ foreach ($supports as $support)
{
$this->writeSupportDirectory($package, $support, $directory);
}
@@ -219,13 +227,7 @@ protected function writeSupportDirectory(Package $package, $support, $directory)
$this->files->put($path.'/config.php', $content);
break;
- case 'routes':
- $content = $this->formatPackageStub($package, $this->files->get(__DIR__.'/stubs/routes.stub'));
- $this->files->put($path.'/routes.php', $content);
- break;
-
default:
- # code...
break;
}
}
@@ -389,4 +391,87 @@ protected function createDirectory(Package $package, $path)
throw new \InvalidArgumentException("Package exists.");
}
+ /**
+ * Create source class directories for package
+ *
+ * @param \Jackiedo\Workbench\Package $package
+ * @param string $directory
+ *
+ * @return void
+ */
+ protected function writeSourceClassDirectories(Package $package, $directory)
+ {
+ $classDirectories = array(
+ 'Console',
+ 'Facades',
+ 'Http',
+ 'Http/Controllers',
+ );
+
+ foreach ($classDirectories as $classDirectory)
+ {
+ $this->writeSourceClassDirectory($package, $classDirectory, $directory);
+ }
+
+ $this->writeMainClassFile($package, $directory);
+ }
+
+ /**
+ * Create specify source class directory
+ *
+ * @param \Jackiedo\Workbench\Package $package
+ * @param string $classDirectory
+ * @param string $directory
+ *
+ * @return void
+ */
+ protected function writeSourceClassDirectory(Package $package, $classDirectory, $directory)
+ {
+ $classDirectoryPath = $this->createClassDirectory($package, $directory);
+
+ // Once we created the class directory, we will create source
+ // class directories and write class files to the these
+ // directories.
+ $path = $classDirectoryPath.'/'.$classDirectory;
+
+ $this->files->makeDirectory($path, 0777, true);
+
+ switch ($classDirectory) {
+ case 'Facades':
+ $content = $this->formatPackageStub($package, $this->files->get(__DIR__.'/stubs/facades.stub'));
+ $this->files->put($path.'/'.$package->name.'.php', $content);
+ break;
+
+ case 'Http':
+ $content = $this->formatPackageStub($package, $this->files->get(__DIR__.'/stubs/routes.stub'));
+ $this->files->put($path.'/routes.php', $content);
+ break;
+
+ case 'Http/Controllers':
+ $content = $this->formatPackageStub($package, $this->files->get(__DIR__.'/stubs/controller.stub'));
+ $this->files->put($path.'/DemoController.php', $content);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ /**
+ * Write main class file for package
+ *
+ * @param \Jackiedo\Workbench\Package $package
+ * @param string $directory
+ *
+ * @return void
+ */
+ protected function writeMainClassFile(Package $package, $directory)
+ {
+ $classDirectoryPath = $this->createClassDirectory($package, $directory);
+
+ $content = $this->formatPackageStub($package, $this->files->get(__DIR__.'/stubs/main.stub'));
+
+ $this->files->put($classDirectoryPath.'/'.$package->name.'.php', $content);
+ }
+
}
View
@@ -4,4 +4,4 @@ return array(
'key' => 'value'
-);
+);
View
@@ -0,0 +1,20 @@
+<?php namespace {{vendor}}\{{name}}\Http\Controllers;
+
+use App\Http\Requests;
+use App\Http\Controllers\Controller;
+use Illuminate\Http\Request;
+use {{vendor}}\{{name}}\Facades\{{name}};
+
+/**
+ * The DemoController facade.
+ *
+ * @package {{vendor}}\{{name}}\Http\Controllers
+ * @author {{author}} <{{email}}>
+ */
+class DemoController extends Controller {
+
+ public function demo() {
+ return {{name}}::welcome();
+ }
+
+}
View
@@ -0,0 +1,22 @@
+<?php namespace {{vendor}}\{{name}}\Facades;
+
+use Illuminate\Support\Facades\Facade;
+
+/**
+ * The {{name}} facade.
+ *
+ * @package {{vendor}}\{{name}}\Facades
+ * @author {{author}} <{{email}}>
+ */
+class {{name}} extends Facade
+{
+ /**
+ * Get the registered name of the component.
+ *
+ * @return string
+ */
+ protected static function getFacadeAccessor()
+ {
+ return '{{lower_name}}';
+ }
+}
View
@@ -0,0 +1,14 @@
+<?php namespace {{vendor}}\{{name}};
+
+/**
+ * The {{name}} facade.
+ *
+ * @package {{vendor}}\{{name}}
+ * @author {{author}} <{{email}}>
+ */
+class {{name}}
+{
+ public function welcome() {
+ return 'Welcome to {{vendor}}\{{name}} package';
+ }
+}
@@ -2,6 +2,12 @@
use Illuminate\Support\ServiceProvider;
+/**
+ * {{name}}ServiceProvider
+ *
+ * @package {{vendor}}\{{name}}
+ * @author {{author}} <{{email}}>
+ */
class {{name}}ServiceProvider extends ServiceProvider {
/**
View
@@ -2,6 +2,12 @@
use Illuminate\Support\ServiceProvider;
+/**
+ * {{name}}ServiceProvider
+ *
+ * @package {{vendor}}\{{name}}
+ * @author {{author}} <{{email}}>
+ */
class {{name}}ServiceProvider extends ServiceProvider {
/**
@@ -18,50 +24,57 @@ class {{name}}ServiceProvider extends ServiceProvider {
*/
public function boot()
{
- /**
- * Loading routes and controllers
- */
- include __DIR__.'/../../routes/routes.php';
+ $packageRoutesPath = __DIR__ . '/Http/routes.php';
+ $packageConfigPath = __DIR__ . '/../../config/config.php';
+ $packageMigrationsPath = __DIR__ . '/../../migrations';
+ $packageAssetsPath = __DIR__ . '/../../resources/assets';
+ $packageTranslationsPath = __DIR__ . '/../../resources/lang';
+ $packageViewsPath = __DIR__ . '/../../resources/views';
/**
- * Loading translations
+ * Loading package's routes and controllers
*/
- $this->loadTranslationsFrom(__DIR__ . '/../../lang', '{{lower_name}}');
+ include $packageRoutesPath;
/**
- * Loading and preparing for publish views
- */
- $this->loadViewsFrom(__DIR__ . '/../../views', '{{lower_name}}');
-
- $this->publishes([
- __DIR__.'/../../views' => base_path('resources/views/{{vendor}}/{{lower_name}}'),
- ], 'views');
-
- /**
- * Preparing for publish config
+ * Publishing package's config
*/
$config = config_path('{{lower_name}}.php');
$this->publishes([
- __DIR__.'/../../config/config.php' => $config,
+ $packageConfigPath => $config,
], 'config');
if (file_exists($config)) {
- $this->mergeConfigFrom($config, '{{lower_name}}');
+ $this->mergeConfigFrom($packageConfigPath, '{{lower_name}}');
}
/**
- * Preparing for publish assets (JavaScript, CSS, images...)
+ * Loading package's translations
+ */
+ $this->loadTranslationsFrom($packageTranslationsPath, '{{lower_name}}');
+
+ /**
+ * Loading and publishing package's views
+ */
+ $this->loadViewsFrom($packageViewsPath, '{{lower_name}}');
+
+ $this->publishes([
+ $packageViewsPath => base_path('resources/views/{{lower_vendor}}/{{lower_name}}'),
+ ], 'views');
+
+ /**
+ * Publishing package's assets (JavaScript, CSS, images...)
*/
$this->publishes([
- __DIR__.'/../../assets' => public_path('packages/{{vendor}}/{{lower_name}}'),
+ $packageAssetsPath => public_path('{{lower_vendor}}/{{lower_name}}'),
], 'public');
/**
- * Preparing for publish your migrations
+ * Publishing package's migrations
*/
$this->publishes([
- __DIR__.'/../../migrations/' => database_path('/migrations')
+ $packageMigrationsPath => database_path('/migrations')
], 'migrations');
}
View
@@ -1,5 +1,3 @@
<?php
-Route::get('{{lower_name}}', function() {
- return 'Welcome to your packages';
-});
+Route::get('{{lower_name}}', '{{vendor}}\{{name}}\Http\Controllers\DemoController@demo');

0 comments on commit 3ce76c5

Please sign in to comment.