Skip to content

Commit

Permalink
Merge pull request #36 from ARCANEDEV/update-1
Browse files Browse the repository at this point in the history
Update PackageServiceProvider
  • Loading branch information
arcanedev-maroc authored Mar 8, 2020
2 parents eae31d6 + d746bae commit 537fe0f
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 27 deletions.
54 changes: 54 additions & 0 deletions src/Providers/Concerns/HasAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Arcanedev\Support\Providers\Concerns;

/**
* Trait HasAssets
*
* @package Arcanedev\Support\Providers\Concerns
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*/
trait HasAssets
{
/* -----------------------------------------------------------------
| Getters & Setters
| -----------------------------------------------------------------
*/

/**
* Get the assets path.
*
* @return string
*/
protected function getAssetsFolder(): string
{
return realpath($this->getBasePath().DIRECTORY_SEPARATOR.'assets');
}

/**
* Get the assets destination path.
*
* @return string
*/
protected function assetsDestinationPath(): string
{
return base_path('assets'.DIRECTORY_SEPARATOR.$this->getPackageName());
}

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

/**
* Publish the assets.
*/
protected function publishAssets(): void
{
$this->publishes([
$this->getAssetsFolder() => $this->assetsDestinationPath(),
], $this->getPublishedTags('assets'));
}
}
59 changes: 52 additions & 7 deletions src/Providers/Concerns/HasConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function getConfigFolder(): string
*/
protected function getConfigKey(): string
{
return Str::slug($this->package);
return Str::slug($this->getPackageName());
}

/**
Expand All @@ -58,7 +58,17 @@ protected function getConfigKey(): string
*/
protected function getConfigFile(): string
{
return $this->getConfigFolder().DIRECTORY_SEPARATOR."{$this->package}.php";
return $this->getConfigFolder().DIRECTORY_SEPARATOR."{$this->getPackageName()}.php";
}

/**
* Get the config files (paths).
*
* @return array|false
*/
protected function configFilesPaths()
{
return glob($this->getConfigFolder().DIRECTORY_SEPARATOR.'*.php');
}

/**
Expand All @@ -70,17 +80,25 @@ protected function registerConfig(string $separator = '.'): void
{
$this->multiConfigs
? $this->registerMultipleConfigs($separator)
: $this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey());
: $this->registerSingleConfig();
}

/**
* Register a single config file.
*/
protected function registerSingleConfig(): void
{
$this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey());
}

/**
* Register all package configs.
*
* @param string $separator
*/
private function registerMultipleConfigs(string $separator = '.'): void
protected function registerMultipleConfigs(string $separator = '.'): void
{
foreach (glob($this->getConfigFolder().'/*.php') as $configPath) {
foreach ($this->configFilesPaths() as $configPath) {
$this->mergeConfigFrom(
$configPath, $this->getConfigKey().$separator.basename($configPath, '.php')
);
Expand All @@ -93,9 +111,36 @@ private function registerMultipleConfigs(string $separator = '.'): void
* @param string|null $path
*/
protected function publishConfig(?string $path = null): void
{
$this->multiConfigs
? $this->publishMultipleConfigs()
: $this->publishSingleConfig($path);
}

/**
* Publish a single config file.
*
* @param string|null $path
*/
protected function publishSingleConfig(?string $path = null): void
{
$this->publishes([
$this->getConfigFile() => $path ?: config_path("{$this->package}.php"),
], [$this->package, 'config', "{$this->package}-config"]);
$this->getConfigFile() => $path ?: config_path("{$this->getPackageName()}.php"),
], $this->getPublishedTags('config'));
}

/**
* Publish multiple config files.
*/
protected function publishMultipleConfigs(): void
{
$paths = [];
$package = $this->getVendorName().DIRECTORY_SEPARATOR.$this->getPackageName();

foreach ($this->configFilesPaths() as $file) {
$paths[$file] = config_path($package.DIRECTORY_SEPARATOR.basename($file));
}

$this->publishes($paths, $this->getPublishedTags('config'));
}
}
2 changes: 1 addition & 1 deletion src/Providers/Concerns/HasFactories.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function publishFactories(?string $path = null): void
{
$this->publishes([
$this->getFactoriesPath() => $path ?: database_path('factories'),
], [$this->package, 'factories', "{$this->package}-factories"]);
], $this->getPublishedTags('factories'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Concerns/HasMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function publishMigrations(?string $path = null): void
{
$this->publishes([
$this->getMigrationsPath() => $path ?: database_path('migrations')
], [$this->package, 'migrations', "{$this->package}-migrations"]);
], $this->getPublishedTags('migrations'));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Providers/Concerns/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function getTranslationsPath(): string
*/
protected function getTranslationsDestinationPath(): string
{
return $this->app['path.lang'].DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.$this->package;
return $this->app['path.lang'].DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.$this->getPackageName();
}

/**
Expand All @@ -46,7 +46,7 @@ protected function publishTranslations(?string $path = null): void
{
$this->publishes([
$this->getTranslationsPath() => $path ?: $this->getTranslationsDestinationPath(),
], [$this->package, 'translations', "{$this->package}-translations"]);
], $this->getPublishedTags('translations'));
}

/**
Expand All @@ -56,7 +56,7 @@ protected function loadTranslations(): void
{
$path = $this->getTranslationsPath();

$this->loadTranslationsFrom($path, $this->package);
$this->loadTranslationsFrom($path, $this->getPackageName());
$this->loadJsonTranslationsFrom($path);
}
}
6 changes: 3 additions & 3 deletions src/Providers/Concerns/HasViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function getViewsPath(): string
*/
protected function getViewsDestinationPath(): string
{
return $this->app['config']['view.paths'][0].DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.$this->package;
return $this->app['config']['view.paths'][0].DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.$this->getPackageName();
}

/**
Expand All @@ -46,14 +46,14 @@ protected function publishViews(?string $path = null): void
{
$this->publishes([
$this->getViewsPath() => $path ?: $this->getViewsDestinationPath(),
], [$this->package, 'views', "{$this->package}-views"]);
], $this->getPublishedTags('views'));
}

/**
* Load the views files.
*/
protected function loadViews(): void
{
$this->loadViewsFrom($this->getViewsPath(), $this->package);
$this->loadViewsFrom($this->getViewsPath(), $this->getPackageName());
}
}
68 changes: 56 additions & 12 deletions src/Providers/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace Arcanedev\Support\Providers;

use Arcanedev\Support\Exceptions\PackageException;
use Arcanedev\Support\Providers\Concerns\{HasConfig, HasFactories, HasMigrations, HasTranslations, HasViews};
use Arcanedev\Support\Providers\Concerns\{
HasAssets, HasConfig, HasFactories, HasMigrations, HasTranslations, HasViews
};
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Str;
use ReflectionClass;

/**
Expand All @@ -22,7 +25,8 @@ abstract class PackageServiceProvider extends ServiceProvider
| -----------------------------------------------------------------
*/

use HasConfig,
use HasAssets,
HasConfig,
HasFactories,
HasMigrations,
HasTranslations,
Expand All @@ -43,9 +47,9 @@ abstract class PackageServiceProvider extends ServiceProvider
/**
* Package name.
*
* @var string
* @var string|null
*/
protected $package = '';
protected $package;

/**
* Package base path.
Expand Down Expand Up @@ -98,6 +102,26 @@ public function getBasePath()
return $this->basePath;
}

/**
* Get the vendor name.
*
* @return string
*/
protected function getVendorName(): string
{
return $this->vendor;
}

/**
* Get the package name.
*
* @return string|null
*/
protected function getPackageName(): ?string
{
return $this->package;
}

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
Expand All @@ -120,16 +144,15 @@ public function register()

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

/* -----------------------------------------------------------------
Expand All @@ -142,10 +165,31 @@ protected function publishAll($load = true)
*
* @throws \Arcanedev\Support\Exceptions\PackageException
*/
private function checkPackageName(): void
protected function checkPackageName(): void
{
if (empty($this->vendor) || empty($this->package)) {
if (empty($this->getVendorName()) || empty($this->getPackageName())) {
throw PackageException::unspecifiedName();
}
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
*/

/**
* Get the published tags.
*
* @param string $tag
*
* @return array
*/
protected function getPublishedTags(string $tag): array
{
$package = $this->getPackageName();

return array_map(function ($name) {
return Str::slug($name);
}, [$package, $tag, $package.'-'.$tag]);
}
}

0 comments on commit 537fe0f

Please sign in to comment.