Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joren Van Hocht authored and Joren Van Hocht committed Sep 15, 2015
2 parents c27253f + 539a527 commit 4998dd7
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 7 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#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
```

#TODO
Learn to write tests and write tests :)
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@
"email": "[email protected]"
}
],
"require": {}
"require": {},
"autoload": {
"files": [
"src/Helpers.php"
]
}
}
12 changes: 12 additions & 0 deletions config/createpackages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => 'yourvendorname',


];
206 changes: 201 additions & 5 deletions src/Commands/CreatePackageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace jorenvanhocht\CreatePackages\Commands;

use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Filesystem\Filesystem as FileSystem;

class CreatePackageCommand extends Command
{
Expand All @@ -11,23 +13,49 @@ 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';

/**
* @var string
*/
protected $package;

/**
* @var string
*/
protected $vendor;

/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;

/**
* @var \Illuminate\Filesystem\Filesystem
*/
protected $filesystem;

/**
* Create a new command instance.
*
* @return void
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Illuminate\Filesystem\Filesystem $filesystem
*/
public function __construct()
public function __construct(Config $config, FileSystem $filesystem)
{
parent::__construct();

$this->config = objectify($config->get('createpackages'));
$this->filesystem = $filesystem;
}

/**
Expand All @@ -37,6 +65,174 @@ public function __construct()
*/
public function handle()
{
//
$this->package = $this->argument('name');
$this->vendor = $this->argument('vendorname');
$this->createFolderStructure();

$this->createServiceProvider();
$this->createFacade();
$this->createPackageClass();

$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);
}

/**
* Get the given vendor name
*
* @return string
*/
private function getVendorName()
{
if(! isset($this->vendor)) {
return $this->config->vendorname;
}

return $this->vendor;
}

/**
* Create the service provider class
*
*/
private function createServiceProvider()
{
$content = file_get_contents(__DIR__.'/../../templates/ServiceProvider.txt');
$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 = $this->replaceNamespace($content, 'Facade');
$content = str_replace('{{packageName2}}', strtolower($this->package), $content);
$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 = $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/"';
}

/**
* 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,';
}

/**
* 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,';
}

/**
* 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());
}
}
15 changes: 15 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

if (! function_exists('objectify') )
{
/**
* Make an object of the given var
*
* @param $var
* @return mixed
*/
function objectify($var)
{
return json_decode(json_encode($var));
}
}
10 changes: 9 additions & 1 deletion src/Providers/CreatePackagesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CreatePackagesServiceProvider extends ServiceProvider {
*/
public function register()
{

//
}

/**
Expand All @@ -21,7 +21,15 @@ public function register()
*/
public function boot()
{
$this->commands([
'jorenvanhocht\CreatePackages\Commands\CreatePackageCommand',
]);

$this->publishes([
__DIR__.'/../../config' => config_path(),
], 'config');

$this->mergeConfigFrom(__DIR__.'/../../config/createpackages.php', 'createpackages');
}

}
13 changes: 13 additions & 0 deletions templates/Facade.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace {{vendorname}}\{{packageName}}\Facades;

use Illuminate\Support\Facades\Facade;

class {{packageName}} extends Facade
{
protected static function getFacadeAccessor()
{
return '{{vendorname}}.{{packageName2}}';
}
}
8 changes: 8 additions & 0 deletions templates/PackageClass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace {{vendorname}}\{{packageName}}\Facades;

class {{packageName}}
{

}
Loading

0 comments on commit 4998dd7

Please sign in to comment.