Skip to content

Commit

Permalink
Adding Service providers for laravel projects
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Aug 18, 2015
1 parent 8cda4fe commit 37cdc4b
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
147 changes: 147 additions & 0 deletions src/Laravel/PackageServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php namespace Arcanedev\Support\Laravel;

use ReflectionClass;

/**
* Class PackageServiceProvider
* @package Arcanedev\Moduly\Bases
*/
abstract class PackageServiceProvider extends ServiceProvider
{
/* ------------------------------------------------------------------------------------------------
| Properties
| ------------------------------------------------------------------------------------------------
*/
/**
* Package name
*
* @var string
*/
protected $package = '';

/**
* Paths collection
*
* @var array
*/
protected $paths = [];

/* ------------------------------------------------------------------------------------------------
| Getters & Setters
| ------------------------------------------------------------------------------------------------
*/
/**
* Get package base path
*
* @return string
*/
protected function getPackagePath()
{
$path = (new ReflectionClass(get_class($this)))
->getFileName();

return dirname(dirname($path));
}

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
*/
public function boot()
{
if (empty($this->package) ) {
throw new \Exception('You must specify the name of the package');
}
}

/**
* Setup package
*
* @param string $path
*
* @return self
*/
public function setup($path)
{
$this->setupPaths($path);

return $this;
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Register all package configs
*
* @return self
*/
protected function registerConfigs()
{
array_map(function ($path) {
$this->mergeConfig($path);
}, glob($this->getPackagePath() . '/config/*.php'));

return $this;
}

/**
* Merge config files
*
* @param string $path
*
* @return self
*/
private function mergeConfig($path)
{
$this->mergeConfigFrom($path,
$this->package . '.' . basename($path, '.php')
);

return $this;
}

/**
* Setup paths
*
* @param string $path
*
* @return self
*/
private function setupPaths($path)
{
$this->paths = [
'migrations' => [
'src' => $path . '/../database/migrations',
'dest' => database_path('/migrations/%s_%s'),
],
'seeds' => [
'src' => $path . '/Seeds',
'dest' => database_path('/seeds/%s'),
],
'config' => [
'src' => $path . '/../config',
'dest' => config_path('%s'),
],
'views' => [
'src' => $path . '/../resources/views',
'dest' => base_path('resources/views/vendor/%s'),
],
'trans' => [
'src' => $path . '/../resources/lang',
'dest' => base_path('resources/lang/%s'),
],
'assets' => [
'src' => $path . '/../resources/assets',
'dest' => public_path('vendor/%s'),
],
'routes' => [
'src' => $path . '/../start/routes.php',
'dest' => null,
],
];

return $this;
}
}
35 changes: 35 additions & 0 deletions src/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php namespace Arcanedev\Support\Laravel;

use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;

/**
* Class ServiceProvider
* @package Arcanedev\Foundation\Bases
*/
abstract class ServiceProvider extends IlluminateServiceProvider
{
/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Add Aliases into the app
*
* @param array $aliases
*
* @return self
*/
protected function addAliases(array $aliases)
{
if (count($aliases)) {
$loader = AliasLoader::getInstance();

foreach ($aliases as $alias => $class) {
$loader->alias($alias, $class);
}
}

return $this;
}
}

0 comments on commit 37cdc4b

Please sign in to comment.