Skip to content

Commit

Permalink
Move reusable code to a trait.
Browse files Browse the repository at this point in the history
Signed-off-by: crynobone <[email protected]>
  • Loading branch information
crynobone committed Feb 14, 2015
1 parent a4a130d commit a14f0d2
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 134 deletions.
5 changes: 5 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ title: Support Change Log

## Version 3.0 {#v3-0}

### v3.0.1 {#v3-0-1}

* Providers:
- Move reusable code from `Orchestra\Support\Providers\ServiceProvider` to `Orchestra\Support\Providers\Traits\PackageProviderTrait`.

### v3.0.0 {#v3-0-0}

* Update support to Laravel Framework v5.0.
Expand Down
136 changes: 2 additions & 134 deletions src/Providers/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,141 +1,9 @@
<?php namespace Orchestra\Support\Providers;

use ReflectionClass;
use Orchestra\Contracts\Config\PackageRepository;
use Orchestra\Support\Providers\Traits\PackageProviderTrait;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

abstract class ServiceProvider extends BaseServiceProvider
{
/**
* Register the package's config component namespaces.
*
* @param string $package
* @param string $namespace
* @param string $path
* @return void
*/
public function addConfigComponent($package, $namespace, $path)
{
$config = $this->app['config'];

if ($config instanceof PackageRepository) {
$config->package($package, $path, $namespace);
}
}

/**
* Register the package's language component namespaces.
*
* @param string $package
* @param string $namespace
* @param string $path
* @return void
*/
public function addLanguageComponent($package, $namespace, $path)
{
$this->app['translator']->addNamespace($namespace, $path);
}

/**
* Register the package's view component namespaces.
*
* @param string $package
* @param string $namespace
* @param string $path
* @return void
*/
public function addViewComponent($package, $namespace, $path)
{
foreach ($this->getAppViewPaths($package) as $appView) {
if ($this->app['files']->isDirectory($appView)) {
$this->app['view']->addNamespace($namespace, $appView);
}
}

// Finally we will register the view namespace so that we can access each of
// the views available in this package. We use a standard convention when
// registering the paths to every package's views and other components.

$this->app['view']->addNamespace($namespace, $path);
}

/**
* Register the package's component namespaces.
*
* @param string $package
* @param string $namespace
* @param string $path
* @return void
*/
public function package($package, $namespace = null, $path = null)
{
$namespace = $this->getPackageNamespace($package, $namespace);
$files = $this->app['files'];

// In this method we will register the configuration package for the package
// so that the configuration options cleanly cascade into the application
// folder to make the developers lives much easier in maintaining them.
$path = $path ?: $this->guessPackagePath();

if ($files->isDirectory($config = $path.'/config')) {
$this->addConfigComponent($package, $namespace, $config);
}

// Next, we will check for any "language" components. If language files exist
// we will register them with this given package's namespace so that they
// may be accessed using the translation facilities of the application.

if ($files->isDirectory($lang = $path.'/lang')) {
$this->addLanguageComponent($package, $namespace, $lang);
}

// Next, we will see if the application view folder contains a folder for the
// package and namespace. If it does, we'll give that folder precedence on
// the loader list for the views so the package views can be overridden.

if ($files->isDirectory($views = $path.'/views')) {
$this->addViewComponent($package, $namespace, $views);
}
}

/**
* Guess the package path for the provider.
*
* @return string
*/
public function guessPackagePath()
{
$path = (new ReflectionClass($this))->getFileName();

return realpath(dirname($path).'/../../');
}

/**
* Determine the namespace for a package.
*
* @param string $package
* @param string $namespace
* @return string
*/
protected function getPackageNamespace($package, $namespace)
{
if (is_null($namespace)) {
list($vendor, $namespace) = explode('/', $package);
}

return $namespace;
}

/**
* Get the application package view paths.
*
* @param string $package
* @return array
*/
protected function getAppViewPaths($package)
{
return array_map(function ($path) use ($package) {
return "{$path}/packages/{$package}";
}, $this->app['config']['view.paths']);
}
use PackageProviderTrait;
}
140 changes: 140 additions & 0 deletions src/Providers/Traits/PackageProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php namespace Orchestra\Support\Providers\Traits;

use ReflectionClass;
use Orchestra\Contracts\Config\PackageRepository;

trait PackageProviderTrait
{
/**
* Register the package's config component namespaces.
*
* @param string $package
* @param string $namespace
* @param string $path
* @return void
*/
public function addConfigComponent($package, $namespace, $path)
{
$config = $this->app['config'];

if ($config instanceof PackageRepository) {
$config->package($package, $path, $namespace);
}
}

/**
* Register the package's language component namespaces.
*
* @param string $package
* @param string $namespace
* @param string $path
* @return void
*/
public function addLanguageComponent($package, $namespace, $path)
{
$this->app['translator']->addNamespace($namespace, $path);
}

/**
* Register the package's view component namespaces.
*
* @param string $package
* @param string $namespace
* @param string $path
* @return void
*/
public function addViewComponent($package, $namespace, $path)
{
foreach ($this->getAppViewPaths($package) as $appView) {
if ($this->app['files']->isDirectory($appView)) {
$this->app['view']->addNamespace($namespace, $appView);
}
}

// Finally we will register the view namespace so that we can access each of
// the views available in this package. We use a standard convention when
// registering the paths to every package's views and other components.

$this->app['view']->addNamespace($namespace, $path);
}

/**
* Register the package's component namespaces.
*
* @param string $package
* @param string $namespace
* @param string $path
* @return void
*/
public function package($package, $namespace = null, $path = null)
{
$namespace = $this->getPackageNamespace($package, $namespace);
$files = $this->app['files'];

// In this method we will register the configuration package for the package
// so that the configuration options cleanly cascade into the application
// folder to make the developers lives much easier in maintaining them.
$path = $path ?: $this->guessPackagePath();

if ($files->isDirectory($config = $path.'/config')) {
$this->addConfigComponent($package, $namespace, $config);
}

// Next, we will check for any "language" components. If language files exist
// we will register them with this given package's namespace so that they
// may be accessed using the translation facilities of the application.

if ($files->isDirectory($lang = $path.'/lang')) {
$this->addLanguageComponent($package, $namespace, $lang);
}

// Next, we will see if the application view folder contains a folder for the
// package and namespace. If it does, we'll give that folder precedence on
// the loader list for the views so the package views can be overridden.

if ($files->isDirectory($views = $path.'/views')) {
$this->addViewComponent($package, $namespace, $views);
}
}

/**
* Guess the package path for the provider.
*
* @return string
*/
public function guessPackagePath()
{
$path = (new ReflectionClass($this))->getFileName();

return realpath(dirname($path).'/../../');
}

/**
* Determine the namespace for a package.
*
* @param string $package
* @param string $namespace
* @return string
*/
protected function getPackageNamespace($package, $namespace)
{
if (is_null($namespace)) {
list(, $namespace) = explode('/', $package);
}

return $namespace;
}

/**
* Get the application package view paths.
*
* @param string $package
* @return array
*/
protected function getAppViewPaths($package)
{
return array_map(function ($path) use ($package) {
return "{$path}/packages/{$package}";
}, $this->app['config']['view.paths']);
}
}

0 comments on commit a14f0d2

Please sign in to comment.