From 692ce59bb3ab8aac35ceff3795dbf1e5a99b7b33 Mon Sep 17 00:00:00 2001 From: Joren Van Hocht Date: Sun, 23 Aug 2015 17:27:42 +0200 Subject: [PATCH 1/7] Register artisan command and define command arguments --- src/Commands/CreatePackageCommand.php | 7 ++++--- src/Providers/CreatePackagesServiceProvider.php | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Commands/CreatePackageCommand.php b/src/Commands/CreatePackageCommand.php index 7dcd10f..8df6962 100644 --- a/src/Commands/CreatePackageCommand.php +++ b/src/Commands/CreatePackageCommand.php @@ -11,19 +11,20 @@ class CreatePackageCommand extends Command * * @var string */ - protected $signature = 'make:package'; + protected $signature = 'make:package + {name : The name of the package} + {vendorname? : Your vendor name (optional)}'; /** * The console command description. * * @var string */ - protected $description = 'Make basic folder structure for a new package.'; + protected $description = 'Create a new package structure'; /** * Create a new command instance. * - * @return void */ public function __construct() { diff --git a/src/Providers/CreatePackagesServiceProvider.php b/src/Providers/CreatePackagesServiceProvider.php index 6743085..294fb86 100644 --- a/src/Providers/CreatePackagesServiceProvider.php +++ b/src/Providers/CreatePackagesServiceProvider.php @@ -12,7 +12,7 @@ class CreatePackagesServiceProvider extends ServiceProvider { */ public function register() { - + // } /** @@ -21,7 +21,9 @@ public function register() */ public function boot() { - + $this->commands([ + 'jorenvanhocht\CreatePackages\Commands\CreatePackageCommand', + ]); } } \ No newline at end of file From 4f3ab7b174cfd3df30c9dcb2dbfd5cf3fad2ac70 Mon Sep 17 00:00:00 2001 From: Joren Van Hocht Date: Sun, 23 Aug 2015 19:38:34 +0200 Subject: [PATCH 2/7] Register config file in service provider and make it publishable --- config/createpackages.php | 12 ++++++++++++ src/Providers/CreatePackagesServiceProvider.php | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/config/createpackages.php b/config/createpackages.php index 25058db..ca9c5d3 100644 --- a/config/createpackages.php +++ b/config/createpackages.php @@ -2,4 +2,16 @@ return [ + /** + * The folder where all packages will be placed + */ + 'folder' => 'packages', + + /** + * The vendor name you want to use for your packages + * + */ + 'vendorname' => 'your-vendor-name', + + ]; \ No newline at end of file diff --git a/src/Providers/CreatePackagesServiceProvider.php b/src/Providers/CreatePackagesServiceProvider.php index 6743085..475c537 100644 --- a/src/Providers/CreatePackagesServiceProvider.php +++ b/src/Providers/CreatePackagesServiceProvider.php @@ -21,7 +21,11 @@ public function register() */ public function boot() { + $this->publishes([ + __DIR__.'/../../config' => config_path(), + ], 'config'); + $this->mergeConfigFrom(__DIR__.'/../../config/createpackages.php', 'createpackages'); } } \ No newline at end of file From dc6365b1b6cab58a6772ac3ff7ddcc07d4804058 Mon Sep 17 00:00:00 2001 From: Joren Van Hocht Date: Tue, 25 Aug 2015 21:23:00 +0200 Subject: [PATCH 3/7] Add first draft for creating empty service provider --- config/createpackages.php | 2 +- src/Commands/CreatePackageCommand.php | 29 +++++++++++++++++++++++++-- templates/ServiceProvider.txt | 28 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 templates/ServiceProvider.txt diff --git a/config/createpackages.php b/config/createpackages.php index ca9c5d3..08b5f85 100644 --- a/config/createpackages.php +++ b/config/createpackages.php @@ -11,7 +11,7 @@ * The vendor name you want to use for your packages * */ - 'vendorname' => 'your-vendor-name', + 'vendorname' => 'yourvendorname', ]; \ No newline at end of file diff --git a/src/Commands/CreatePackageCommand.php b/src/Commands/CreatePackageCommand.php index 2828006..989f395 100644 --- a/src/Commands/CreatePackageCommand.php +++ b/src/Commands/CreatePackageCommand.php @@ -68,17 +68,32 @@ public function handle() $this->package = $this->argument('name'); $this->vendor = $this->argument('vendorname'); $this->createFolderStructure(); + + // create empty service provider + $this->createServiceProvider(); + // create empty facade + // create empty package class + + // return psr4 autoloading line for in composer.json file + // return service provider for in config/app.php + // return facade for in config/app.php } private function createFolderStructure() { $baseFolder = $this->config->folder; $vendorname = $this->getVendorName(); - $fullPath = "$baseFolder/$vendorname/$this->package"; + $basePath = "$baseFolder/$vendorname/$this->package"; - $this->filesystem->makeDirectory($fullPath, 0755, true); + $this->filesystem->makeDirectory($basePath, 0755, true); + $this->filesystem->makeDirectory("$basePath/src", 0755, true); + $this->filesystem->makeDirectory("$basePath/tests", 0755, true); + $this->filesystem->makeDirectory("$basePath/Providers", 0755, true); } + /** + * @return string + */ private function getVendorName() { if(! isset($this->vendor)) { @@ -87,4 +102,14 @@ private function getVendorName() return $this->vendor; } + + private function createServiceProvider() + { + $content = file_get_contents(__DIR__.'/../../templates/ServiceProvider.txt'); + $content = str_replace('{{vendorname}}', $this->getVendorName(), $content); + $content = str_replace('{{packageName}}', ucfirst($this->package).'ServicePovider', $content); + $file = $this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/Providers/'.ucfirst($this->package).'ServiceProvider.php'; + + file_put_contents($file, $content); + } } diff --git a/templates/ServiceProvider.txt b/templates/ServiceProvider.txt new file mode 100644 index 0000000..4c2a1cc --- /dev/null +++ b/templates/ServiceProvider.txt @@ -0,0 +1,28 @@ + Date: Thu, 27 Aug 2015 21:38:47 +0200 Subject: [PATCH 4/7] Create missing classes and write output to the user --- src/Commands/CreatePackageCommand.php | 52 +++++++++++++++++++++++++-- templates/Facade.txt | 13 +++++++ templates/PackageClass.txt | 8 +++++ templates/ServiceProvider.txt | 2 +- 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 templates/Facade.txt create mode 100644 templates/PackageClass.txt diff --git a/src/Commands/CreatePackageCommand.php b/src/Commands/CreatePackageCommand.php index 989f395..978b19f 100644 --- a/src/Commands/CreatePackageCommand.php +++ b/src/Commands/CreatePackageCommand.php @@ -72,11 +72,22 @@ public function handle() // create empty service provider $this->createServiceProvider(); // create empty facade + $this->createFacade(); // create empty package class + $this->createPackageClass(); + $this->info('Folder structure and classes sucesfully created'); // return psr4 autoloading line for in composer.json file + $this->info('Add this line to the PRS-4 autoloading section in your composer.json file'); + $this->info($this->getComposerJsonAutoloading()); // return service provider for in config/app.php + $this->info(''); + $this->info('Add this line to the providers array in config/app.php'); + $this->info($this->getServiceProvider()); // return facade for in config/app.php + $this->info(''); + $this->info('Add this line to the aliases array in config/app.php'); + $this->info($this->getFacade()); } private function createFolderStructure() @@ -88,7 +99,8 @@ private function createFolderStructure() $this->filesystem->makeDirectory($basePath, 0755, true); $this->filesystem->makeDirectory("$basePath/src", 0755, true); $this->filesystem->makeDirectory("$basePath/tests", 0755, true); - $this->filesystem->makeDirectory("$basePath/Providers", 0755, true); + $this->filesystem->makeDirectory("$basePath/src/Providers", 0755, true); + $this->filesystem->makeDirectory("$basePath/src/Facades", 0755, true); } /** @@ -108,8 +120,44 @@ private function createServiceProvider() $content = file_get_contents(__DIR__.'/../../templates/ServiceProvider.txt'); $content = str_replace('{{vendorname}}', $this->getVendorName(), $content); $content = str_replace('{{packageName}}', ucfirst($this->package).'ServicePovider', $content); - $file = $this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/Providers/'.ucfirst($this->package).'ServiceProvider.php'; + $file = $this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/src/Providers/'.ucfirst($this->package).'ServiceProvider.php'; file_put_contents($file, $content); } + + private function createFacade() + { + $content = file_get_contents(__DIR__.'/../../templates/Facade.txt'); + $content = str_replace('{{vendorname}}', $this->getVendorName(), $content); + $content = str_replace('{{packageName}}', ucfirst($this->package).'Facade', $content); + $content = str_replace('{{packageName2}}', strtolower($this->package), $content); + $file = $this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/src/Facades/'.ucfirst($this->package).'Facade.php'; + + file_put_contents($file, $content); + } + + private function createPackageClass() + { + $content = file_get_contents(__DIR__.'/../../templates/PackageClass.txt'); + $content = str_replace('{{vendorname}}', $this->getVendorName(), $content); + $content = str_replace('{{packageName}}', ucfirst($this->package), $content); + $file = $this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/src/'.ucfirst($this->package).'.php'; + + file_put_contents($file, $content); + } + + private function getComposerJsonAutoloading() + { + return '"'.$this->getVendorName().'\\\\'.ucfirst($this->package).'": "'.$this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/src/"'; + } + + private function getServiceProvider() + { + return $this->getVendorName().'\\'.ucfirst($this->package).'\Providers\\'.ucfirst($this->package).'ServiceProvider::class,'; + } + + private function getFacade() + { + return $this->getVendorName().'\\'.ucfirst($this->package).'\Facades\\'.ucfirst($this->package).'Facade::class,'; + } } diff --git a/templates/Facade.txt b/templates/Facade.txt new file mode 100644 index 0000000..356833d --- /dev/null +++ b/templates/Facade.txt @@ -0,0 +1,13 @@ + Date: Mon, 31 Aug 2015 21:29:02 +0200 Subject: [PATCH 5/7] Add readme and refactored code --- README.md | 43 ++++++++ src/Commands/CreatePackageCommand.php | 145 +++++++++++++++++++------- 2 files changed, 153 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index e69de29..4edd2ed 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,43 @@ +#Create packages + +This package is inspired by the workbench package that came default in Laravel 4. +It speeds up your workflow for creating packages, once you have set your config settings the only thing left +is running the artisan command and start developing your package. + +##Installation + +You can install this package through composer by running the following command + +```php + $ composer require jorenvanhocht\create-packages 1.0 +``` + +Now add the service provider to the provider array in ```config/app.php``` + +```php + jorenvanhocht\CreatePackages\Providers\CreatePackagesServiceProvider::class, +``` + +##Configuration + +Publish the config file by running the following command from your terminal + +```php + $ php artisan vendor:publish +``` + +Set your base folder and your vendor name, and you are good to go. + +##Usage + +To create a new package run + +```php + $ php artisan make:package yourPackageName +``` + +If want to create a package with a different vendor name then set in your config file you can add it as a parameter + +```php + $ php artisan make:package yourPackageName YourNewVendorName +``` \ No newline at end of file diff --git a/src/Commands/CreatePackageCommand.php b/src/Commands/CreatePackageCommand.php index 978b19f..c453528 100644 --- a/src/Commands/CreatePackageCommand.php +++ b/src/Commands/CreatePackageCommand.php @@ -69,41 +69,38 @@ public function handle() $this->vendor = $this->argument('vendorname'); $this->createFolderStructure(); - // create empty service provider $this->createServiceProvider(); - // create empty facade $this->createFacade(); - // create empty package class $this->createPackageClass(); - $this->info('Folder structure and classes sucesfully created'); - // return psr4 autoloading line for in composer.json file - $this->info('Add this line to the PRS-4 autoloading section in your composer.json file'); - $this->info($this->getComposerJsonAutoloading()); - // return service provider for in config/app.php - $this->info(''); - $this->info('Add this line to the providers array in config/app.php'); - $this->info($this->getServiceProvider()); - // return facade for in config/app.php - $this->info(''); - $this->info('Add this line to the aliases array in config/app.php'); - $this->info($this->getFacade()); + $this->returnOutput(); } + /** + * Create the package directories + * + */ private function createFolderStructure() { $baseFolder = $this->config->folder; $vendorname = $this->getVendorName(); $basePath = "$baseFolder/$vendorname/$this->package"; - $this->filesystem->makeDirectory($basePath, 0755, true); - $this->filesystem->makeDirectory("$basePath/src", 0755, true); - $this->filesystem->makeDirectory("$basePath/tests", 0755, true); - $this->filesystem->makeDirectory("$basePath/src/Providers", 0755, true); - $this->filesystem->makeDirectory("$basePath/src/Facades", 0755, true); + $this->filesystem + ->makeDirectory($basePath, 0755, true); + $this->filesystem + ->makeDirectory("$basePath/src", 0755, true); + $this->filesystem + ->makeDirectory("$basePath/tests", 0755, true); + $this->filesystem + ->makeDirectory("$basePath/src/Providers", 0755, true); + $this->filesystem + ->makeDirectory("$basePath/src/Facades", 0755, true); } /** + * Get the given vendor name + * * @return string */ private function getVendorName() @@ -115,49 +112,127 @@ private function getVendorName() return $this->vendor; } + /** + * Create the service provider class + * + */ private function createServiceProvider() { $content = file_get_contents(__DIR__.'/../../templates/ServiceProvider.txt'); - $content = str_replace('{{vendorname}}', $this->getVendorName(), $content); - $content = str_replace('{{packageName}}', ucfirst($this->package).'ServicePovider', $content); - $file = $this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/src/Providers/'.ucfirst($this->package).'ServiceProvider.php'; - + $content = $this->replaceNamespace($content, 'ServiceProvider'); + $file = $this->getFilePath('Providers', 'ServiceProvider'); file_put_contents($file, $content); } + /** + * Create the facade class + * + */ private function createFacade() { $content = file_get_contents(__DIR__.'/../../templates/Facade.txt'); - $content = str_replace('{{vendorname}}', $this->getVendorName(), $content); - $content = str_replace('{{packageName}}', ucfirst($this->package).'Facade', $content); + $content = $this->replaceNamespace($content, 'Facade'); $content = str_replace('{{packageName2}}', strtolower($this->package), $content); - $file = $this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/src/Facades/'.ucfirst($this->package).'Facade.php'; - + $file = $this->getFilePath('Facades', 'Facade'); file_put_contents($file, $content); } + /** + * Create the base package class + * + */ private function createPackageClass() { $content = file_get_contents(__DIR__.'/../../templates/PackageClass.txt'); - $content = str_replace('{{vendorname}}', $this->getVendorName(), $content); - $content = str_replace('{{packageName}}', ucfirst($this->package), $content); - $file = $this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/src/'.ucfirst($this->package).'.php'; - + $content = $this->replaceNamespace($content, ''); + $file = $this->getFilePath('', ''); file_put_contents($file, $content); } + /** + * Generate the psr4 autoloading line that needs to be added + * to the composer.json file of the project + * + * @return string + */ private function getComposerJsonAutoloading() { - return '"'.$this->getVendorName().'\\\\'.ucfirst($this->package).'": "'.$this->config->folder.'/'.$this->getVendorName().'/'.$this->package.'/src/"'; + return '"'.$this->getVendorName().'\\\\'.ucfirst($this->package). + '": "'.$this->config->folder.'/'.$this->getVendorName().'/'. + $this->package.'/src/"'; } + /** + * Get the service provider that needs to be added to + * the config/app.php file + * + * @return string + */ private function getServiceProvider() { - return $this->getVendorName().'\\'.ucfirst($this->package).'\Providers\\'.ucfirst($this->package).'ServiceProvider::class,'; + return $this->getVendorName().'\\'.ucfirst($this->package). + '\Providers\\'.ucfirst($this->package). + 'ServiceProvider::class,'; } + /** + * Get the alias that needs to be added to + * the config/app.php file + * + * @return string + */ private function getFacade() { - return $this->getVendorName().'\\'.ucfirst($this->package).'\Facades\\'.ucfirst($this->package).'Facade::class,'; + return $this->getVendorName().'\\'.ucfirst($this->package).'\Facades\\'. + ucfirst($this->package).'Facade::class,'; + } + + /** + * Fill in the correct namespace in a template file + * + * @param $content + * @param $folder + * @return mixed + */ + private function replaceNamespace($content, $folder) + { + $content = str_replace('{{vendorname}}', $this->getVendorName(), $content); + $content = str_replace('{{packageName}}', ucfirst($this->package). $folder , $content); + + return $content; + } + + /** + * Get the full path where the generated file needs to be placed + * + * @param $folder + * @param $suffix + * @return string + */ + private function getFilePath($folder, $suffix) + { + return $this->config->folder.'/'.$this->getVendorName().'/'. + $this->package."/src/$folder/".ucfirst($this->package). + "$suffix.php"; + } + + /** + * Output info to the user + * + */ + private function returnOutput() + { + $this->comment('Folder structure and classes sucesfully created'); + + $this->comment('Add this line to the PRS-4 autoloading section in your composer.json file'); + $this->info($this->getComposerJsonAutoloading()); + + $this->info(''); + $this->comment('Add this line to the providers array in config/app.php'); + $this->info($this->getServiceProvider()); + + $this->info(''); + $this->comment('Add this line to the aliases array in config/app.php'); + $this->info($this->getFacade()); } } From aeeb7091db4b29a7411bb04d973cc88715cb9b16 Mon Sep 17 00:00:00 2001 From: Joren Van Hocht Date: Mon, 31 Aug 2015 21:30:23 +0200 Subject: [PATCH 6/7] Delete tabs --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4edd2ed..42429fa 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,13 @@ is running the artisan command and start developing your package. You can install this package through composer by running the following command ```php - $ composer require jorenvanhocht\create-packages 1.0 +$ composer require jorenvanhocht\create-packages 1.0 ``` Now add the service provider to the provider array in ```config/app.php``` ```php - jorenvanhocht\CreatePackages\Providers\CreatePackagesServiceProvider::class, +jorenvanhocht\CreatePackages\Providers\CreatePackagesServiceProvider::class, ``` ##Configuration @@ -23,7 +23,7 @@ Now add the service provider to the provider array in ```config/app.php``` Publish the config file by running the following command from your terminal ```php - $ php artisan vendor:publish +$ php artisan vendor:publish ``` Set your base folder and your vendor name, and you are good to go. @@ -33,11 +33,11 @@ Set your base folder and your vendor name, and you are good to go. To create a new package run ```php - $ php artisan make:package yourPackageName +$ php artisan make:package yourPackageName ``` If want to create a package with a different vendor name then set in your config file you can add it as a parameter ```php - $ php artisan make:package yourPackageName YourNewVendorName +$ php artisan make:package yourPackageName YourNewVendorName ``` \ No newline at end of file From fe868760575fd50f8b978a60a1fdf4902709d6ed Mon Sep 17 00:00:00 2001 From: Joren Van Hocht Date: Tue, 15 Sep 2015 21:20:14 +0200 Subject: [PATCH 7/7] Add todo to readme file --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 42429fa..dfdfc58 100644 --- a/README.md +++ b/README.md @@ -40,4 +40,7 @@ If want to create a package with a different vendor name then set in your config ```php $ php artisan make:package yourPackageName YourNewVendorName -``` \ No newline at end of file +``` + +#TODO +Learn to write tests and write tests :) \ No newline at end of file