Permalink
Browse files

Merge branch 'add-view-publish-command' of https://github.com/alexrus…

…sell/framework into alexrussell-add-view-publish-command
  • Loading branch information...
taylorotwell committed Oct 1, 2013
2 parents a1b9358 + e66979c commit 44a27da7245b5da88fd97934d250b59813127053
@@ -22,9 +22,9 @@ class ConfigPublishCommand extends Command {
protected $description = "Publish a package's configuration to the application";
/**
* The asset publisher instance.
* The config publisher instance.
*
* @var \Illuminate\Foundation\AssetPublisher
* @var \Illuminate\Foundation\ConfigPublisher
*/
protected $config;
@@ -101,4 +101,4 @@ protected function getOptions()
);
}
}
}
@@ -0,0 +1,104 @@
<?php namespace Illuminate\Foundation\Console;
use Illuminate\Console\Command;
use Illuminate\Foundation\ViewPublisher;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ViewPublishCommand extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'view:publish';
/**
* The console command description.
*
* @var string
*/
protected $description = "Publish a package's views to the application";
/**
* The view publisher instance.
*
* @var \Illuminate\Foundation\ViewPublisher
*/
protected $view;
/**
* Create a new view publish command instance.
*
* @param \Illuminate\Foundation\ViewPublisher $view
* @return void
*/
public function __construct(ViewPublisher $view)
{
parent::__construct();
$this->view = $view;
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$package = $this->input->getArgument('package');
if ( ! is_null($path = $this->getPath()))
{
$this->view->publish($package, $path);
}
else
{
$this->view->publishPackage($package);
}
$this->output->writeln('<info>Views published for package:</info> '.$package);
}
/**
* Get the specified path to the files.
*
* @return string
*/
protected function getPath()
{
$path = $this->input->getOption('path');
if ( ! is_null($path))
{
return $this->laravel['path.base'].'/'.$path;
}
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('package', InputArgument::REQUIRED, 'The name of package being published.'),
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('path', null, InputOption::VALUE_OPTIONAL, 'The path to the view files.', null),
);
}
}
@@ -3,8 +3,10 @@
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\AssetPublisher;
use Illuminate\Foundation\ConfigPublisher;
use Illuminate\Foundation\ViewPublisher;
use Illuminate\Foundation\Console\AssetPublishCommand;
use Illuminate\Foundation\Console\ConfigPublishCommand;
use Illuminate\Foundation\Console\ViewPublishCommand;
class PublisherServiceProvider extends ServiceProvider {
@@ -26,7 +28,9 @@ public function register()
$this->registerConfigPublisher();
$this->commands('command.asset.publish', 'command.config.publish');
$this->registerViewPublisher();
$this->commands('command.asset.publish', 'command.config.publish', 'command.view.publish');
}
/**
@@ -103,6 +107,43 @@ protected function registerConfigPublishCommand()
});
}
/**
* Register the view publisher class and command.
*
* @return void
*/
protected function registerViewPublisher()
{
$this->registerViewPublishCommand();
$this->app['view.publisher'] = $this->app->share(function($app)
{
$viewPath = $app['path'].'/views';
// Once we have created the view publisher, we will set the default
// package path on the object so that it knows where to find the packages
// that are installed for the application and can move them to the app.
$publisher = new ViewPublisher($app['files'], $viewPath);
$publisher->setPackagePath($app['path.base'].'/vendor');
return $publisher;
});
}
/**
* Register the view publish console command.
*
* @return void
*/
protected function registerViewPublishCommand()
{
$this->app['command.view.publish'] = $this->app->share(function($app)
{
return new ViewPublishCommand($app['view.publisher']);
});
}
/**
* Get the services provided by the provider.
*
@@ -114,7 +155,9 @@ public function provides()
'asset.publisher',
'command.asset.publish',
'config.publisher',
'command.config.publish'
'command.config.publish',
'view.publisher',
'command.view.publish'
);
}
@@ -0,0 +1,123 @@
<?php namespace Illuminate\Foundation;
use Illuminate\Filesystem\Filesystem;
class ViewPublisher {
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The destination of the view files.
*
* @var string
*/
protected $publishPath;
/**
* The path to the application's packages.
*
* @var string
*/
protected $packagePath;
/**
* Create a new view publisher instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $publishPath
* @return void
*/
public function __construct(Filesystem $files, $publishPath)
{
$this->files = $files;
$this->publishPath = $publishPath;
}
/**
* Publish view files from a given path.
*
* @param string $package
* @param string $source
* @return void
*/
public function publish($package, $source)
{
$destination = $this->publishPath."/{$package}";
$this->makeDestination($destination);
return $this->files->copyDirectory($source, $destination);
}
/**
* Publish the view files for a package.
*
* @param string $package
* @param string $packagePath
* @return void
*/
public function publishPackage($package, $packagePath = null)
{
list($vendor, $name) = explode('/', $package);
// First we will figure out the source of the package's views location
// which we do by convention. Once we have that we will move the files over
// to the "main" views directory for this particular application.
$path = $packagePath ?: $this->packagePath;
$source = $this->getSource($package, $name, $path);
return $this->publish($package, $source);
}
/**
* Get the source views directory to publish.
*
* @param string $package
* @param string $name
* @param string $packagePath
* @return string
*/
protected function getSource($package, $name, $packagePath)
{
$source = $packagePath."/{$package}/src/views";
if ( ! $this->files->isDirectory($source))
{
throw new \InvalidArgumentException("Views not found.");
}
return $source;
}
/**
* Create the destination directory if it doesn't exist.
*
* @param string $destination
* @return void
*/
protected function makeDestination($destination)
{
if ( ! $this->files->isDirectory($destination))
{
$this->files->makeDirectory($destination, 0777, true);
}
}
/**
* Set the default package path.
*
* @param string $packagePath
* @return void
*/
public function setPackagePath($packagePath)
{
$this->packagePath = $packagePath;
}
}
@@ -0,0 +1,20 @@
<?php
use Mockery as m;
class FoundationViewPublishCommandTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
m::close();
}
public function testCommandCallsPublisherWithProperPackageName()
{
$command = new Illuminate\Foundation\Console\ViewPublishCommand($pub = m::mock('Illuminate\Foundation\ViewPublisher'));
$pub->shouldReceive('publishPackage')->once()->with('foo');
$command->run(new Symfony\Component\Console\Input\ArrayInput(array('package' => 'foo')), new Symfony\Component\Console\Output\NullOutput);
}
}
@@ -0,0 +1,31 @@
<?php
use Mockery as m;
class FoundationViewPublisherTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
m::close();
}
public function testPackageViewPublishing()
{
$pub = new Illuminate\Foundation\ViewPublisher($files = m::mock('Illuminate\Filesystem\Filesystem'), __DIR__);
$pub->setPackagePath(__DIR__.'/vendor');
$files->shouldReceive('isDirectory')->once()->with(__DIR__.'/vendor/foo/bar/src/views')->andReturn(true);
$files->shouldReceive('isDirectory')->once()->with(__DIR__.'/foo/bar')->andReturn(true);
$files->shouldReceive('copyDirectory')->once()->with(__DIR__.'/vendor/foo/bar/src/views', __DIR__.'/foo/bar')->andReturn(true);
$this->assertTrue($pub->publishPackage('foo/bar'));
$pub = new Illuminate\Foundation\ViewPublisher($files2 = m::mock('Illuminate\Filesystem\Filesystem'), __DIR__);
$files2->shouldReceive('isDirectory')->once()->with(__DIR__.'/custom-packages/foo/bar/src/views')->andReturn(true);
$files2->shouldReceive('isDirectory')->once()->with(__DIR__.'/foo/bar')->andReturn(true);
$files2->shouldReceive('copyDirectory')->once()->with(__DIR__.'/custom-packages/foo/bar/src/views', __DIR__.'/foo/bar')->andReturn(true);
$this->assertTrue($pub->publishPackage('foo/bar', __DIR__.'/custom-packages'));
}
}

0 comments on commit 44a27da

Please sign in to comment.