Skip to content

Commit

Permalink
Merge pull request #30 from ARCANEDEV/update-package
Browse files Browse the repository at this point in the history
Updating service providers & composer.json
  • Loading branch information
arcanedev-maroc authored Oct 2, 2019
2 parents cb32ca2 + 86751f1 commit f4aa28e
Show file tree
Hide file tree
Showing 15 changed files with 572 additions and 441 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"license": "MIT",
"require": {
"php": ">=7.2.0",
"illuminate/filesystem": "~6.0.0",
"illuminate/support": "~6.0.0"
"illuminate/filesystem": "^6.0",
"illuminate/support": "^6.0"
},
"require-dev": {
"orchestra/testbench": "~4.0.0",
"phpunit/phpcov": "~6.0",
"phpunit/phpunit": "~8.0"
"orchestra/testbench": "^4.0",
"phpunit/phpcov": "^6.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
Expand Down
365 changes: 4 additions & 361 deletions src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,371 +1,14 @@
<?php namespace Arcanedev\Support;

use Arcanedev\Support\Exceptions\PackageException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Str;
use ReflectionClass;

/**
* Class PackageServiceProvider
*
* @package Arcanedev\Support\Laravel
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*
* @deprecated Use `Arcanedev\Support\Providers\PackageServiceProvider` instead
*/
abstract class PackageServiceProvider extends ServiceProvider
abstract class PackageServiceProvider extends Providers\PackageServiceProvider
{
/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/

/**
* Vendor name.
*
* @var string
*/
protected $vendor = 'arcanedev';

/**
* Package name.
*
* @var string
*/
protected $package = '';

/**
* Package base path.
*
* @var string
*/
protected $basePath;

/**
* Merge multiple config files into one instance (package name as root key)
*
* @var bool
*/
protected $multiConfigs = false;

/* -----------------------------------------------------------------
| Constructor
| -----------------------------------------------------------------
*/

/**
* Create a new service provider instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function __construct(Application $app)
{
parent::__construct($app);

$this->basePath = $this->resolveBasePath();
}

/**
* Resolve the base path of the package.
*
* @return string
*/
protected function resolveBasePath()
{
return dirname(
(new ReflectionClass($this))->getFileName(), 2
);
}

/* -----------------------------------------------------------------
| Getters & Setters
| -----------------------------------------------------------------
*/

/**
* Get the base path of the package.
*
* @return string
*/
public function getBasePath()
{
return $this->basePath;
}

/**
* Get config folder.
*
* @return string
*/
protected function getConfigFolder()
{
return realpath($this->getBasePath().DS.'config');
}

/**
* Get config key.
*
* @return string
*/
protected function getConfigKey()
{
return Str::slug($this->package);
}

/**
* Get config file path.
*
* @return string
*/
protected function getConfigFile()
{
return $this->getConfigFolder().DS."{$this->package}.php";
}

/**
* Get config file destination path.
*
* @return string
*/
protected function getConfigFileDestination()
{
return config_path("{$this->package}.php");
}

/**
* Get the base database path.
*
* @return string
*/
protected function getDatabasePath()
{
return $this->getBasePath().DS.'database';
}

/**
* Get the migrations path.
*
* @return string
*/
protected function getMigrationsPath()
{
return $this->getBasePath().DS.'database'.DS.'migrations';
}

/**
* Get the base resources path.
*
* @return string
*/
protected function getResourcesPath()
{
return $this->getBasePath().DS.'resources';
}

/**
* Get the base views path.
*
* @return string
*/
protected function getViewsPath()
{
return $this->getResourcesPath().DS.'views';
}

/**
* Get the destination views path.
*
* @return string
*/
protected function getViewsDestinationPath()
{
return resource_path('views'.DS.'vendor'.DS.$this->package);
}

/**
* Get the translations path.
*
* @return string
*/
protected function getTranslationsPath()
{
return $this->getResourcesPath().DS.'lang';
}

/**
* Get the destination views path.
*
* @return string
*/
protected function getTranslationsDestinationPath()
{
return resource_path('lang'.DS.'vendor'.DS.$this->package);
}

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Register the service provider.
*/
public function register()
{
parent::register();

$this->checkPackageName();
}

/* -----------------------------------------------------------------
| Package Methods
| -----------------------------------------------------------------
*/

/**
* Register configs.
*
* @param string $separator
*/
protected function registerConfig($separator = '.')
{
$this->multiConfigs
? $this->registerMultipleConfigs($separator)
: $this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey());
}

/**
* Register all package configs.
*
* @param string $separator
*/
private function registerMultipleConfigs($separator = '.')
{
foreach (glob($this->getConfigFolder().'/*.php') as $configPath) {
$this->mergeConfigFrom(
$configPath, $this->getConfigKey().$separator.basename($configPath, '.php')
);
}
}

/**
* Register commands service provider.
*
* @param \Illuminate\Support\ServiceProvider|string $provider
*/
protected function registerCommands($provider)
{
if ($this->app->runningInConsole())
$this->app->register($provider);
}

/**
* Publish the config file.
*/
protected function publishConfig()
{
$this->publishes([
$this->getConfigFile() => $this->getConfigFileDestination()
], 'config');
}

/**
* Publish the migration files.
*/
protected function publishMigrations()
{
$this->publishes([
$this->getMigrationsPath() => database_path('migrations')
], 'migrations');
}

/**
* Publish and load the views if $load argument is true.
*
* @param bool $load
*/
protected function publishViews($load = true)
{
$this->publishes([
$this->getViewsPath() => $this->getViewsDestinationPath()
], 'views');

if ($load) $this->loadViews();
}

/**
* Publish and load the translations if $load argument is true.
*
* @param bool $load
*/
protected function publishTranslations($load = true)
{
$this->publishes([
$this->getTranslationsPath() => $this->getTranslationsDestinationPath()
], 'lang');

if ($load) $this->loadTranslations();
}

/**
* Publish the factories.
*/
protected function publishFactories()
{
$this->publishes([
$this->getDatabasePath().DS.'factories' => database_path('factories'),
], 'factories');
}

/**
* Publish all the package files.
*
* @param bool $load
*/
protected function publishAll($load = true)
{
$this->publishConfig();
$this->publishMigrations();
$this->publishViews($load);
$this->publishTranslations($load);
$this->publishFactories();
}

/**
* Load the views files.
*/
protected function loadViews()
{
$this->loadViewsFrom($this->getViewsPath(), $this->package);
}

/**
* Load the translations files.
*/
protected function loadTranslations()
{
$this->loadTranslationsFrom($this->getTranslationsPath(), $this->package);
}

/**
* Load the migrations files.
*/
protected function loadMigrations()
{
$this->loadMigrationsFrom($this->getMigrationsPath());
}

/* ------------------------------------------------------------------------------------------------
| Check Functions
| ------------------------------------------------------------------------------------------------
*/

/**
* Check package name.
*
* @throws \Arcanedev\Support\Exceptions\PackageException
*/
private function checkPackageName()
{
if (empty($this->vendor) || empty($this->package))
throw new PackageException('You must specify the vendor/package name.');
}
//
}
Loading

0 comments on commit f4aa28e

Please sign in to comment.