merged skunkworks into develop.

This commit is contained in:
Taylor Otwell
2012-01-16 13:59:24 -06:00
parent 610d8827c4
commit b5442c67fc
117 changed files with 7268 additions and 3999 deletions

View File

@@ -0,0 +1,127 @@
<?php namespace Laravel\CLI\Tasks\Bundle; defined('APP_PATH') or die('No direct script access.');
use Laravel\IoC;
use Laravel\Bundle;
use Laravel\CLI\Tasks\Task;
IoC::singleton('bundle.repository', function()
{
return new Repository;
});
IoC::singleton('bundle.publisher', function()
{
return new Publisher;
});
IoC::singleton('bundle.provider: github', function()
{
return new Providers\Github;
});
class Bundler extends Task {
/**
* Install the given bundles into the application.
*
* @param array $bundles
* @return void
*/
public function install($bundles)
{
$publisher = IoC::resolve('bundle.publisher');
foreach ($this->get($bundles) as $bundle)
{
if (is_dir(BUNDLE_PATH.$bundle['name']))
{
echo "Bundle {$bundle['name']} is already installed.";
continue;
}
// Once we have the bundle information, we can resolve an instance
// of a provider and install the bundle into the application and
// all of its registered dependencies as well.
//
// Each bundle provider implements the Provider interface and
// is repsonsible for retrieving the bundle source from its
// hosting party and installing it into the application.
$provider = "bundle.provider: {$bundle['provider']}";
IoC::resolve($provider)->install($bundle);
$publisher->publish($bundle);
}
}
/**
* Publish bundle assets to the public directory.
*
* @param array $bundles
* @return void
*/
public function publish($bundles)
{
// If no bundles are passed to the command, we'll just gather all
// of the installed bundle names and publish the assets for each
// for each one of the bundles to the public directory.
if (count($bundles) == 0) $bundles = Bundle::all();
$publisher = IoC::resolve('bundle.publisher');
foreach ($bundles as $bundle)
{
$publisher->publish($bundle);
}
}
/**
* Gather all of the bundles from the bundle repository.
*
* @param array $bundles
* @return array
*/
protected function get($bundles)
{
$responses = array();
$repository = IoC::resolve('bundle.repository');
// This method is primarily responsible for gathering the data
// for all bundles that need to be installed. This allows us
// to verify the existence of the bundle before even getting
// started on the actual installation process.
foreach ($bundles as $bundle)
{
// First we'll call the bundle repository to gather the bundle data
// array, which contains all of the information needed to install
// the bundle into the application. We'll verify that the bundle
// exists and the API is responding for each bundle.
$response = $repository->get($bundle);
if ( ! $response)
{
throw new \Exception("The bundle API is not responding.");
}
if ($response['status'] == 'not-found')
{
throw new \Exception("There is not a bundle named [$bundle].");
}
// If the bundle was retrieved successfully, we will add it to
// our array of bundles, as well as merge all of the bundle's
// dependencies into the array of responses so that they are
// installed along with the consuming dependency.
$bundle = $response['bundle'];
$responses[] = $bundle;
$responses = array_merge($responses, $this->get($bundle['dependencies']));
}
return $responses;
}
}

View File

@@ -0,0 +1,27 @@
<?php namespace Laravel\CLI\Tasks\Bundle\Providers;
class Github implements Provider {
/**
* Install the given bundle into the application.
*
* @param string $bundle
* @return void
*/
public function install($bundle)
{
$repository = "git://github.com/{$bundle['location']}.git";
// We need to just extract the basename of the bundle path when
// adding the submodule. Of course, we can't add a submodule to
// a location outside of the Git repository, so we don't need
// the full bundle path. We'll just take the basename in case
// the bundle directory has been renamed.
$path = basename(BUNDLE_PATH).'/';
passthru('git submodule add '.$repository.' '.$path.$bundle['name']);
passthru('git submodule update');
}
}

View File

@@ -0,0 +1,13 @@
<?php namespace Laravel\CLI\Tasks\Bundle\Providers;
interface Provider {
/**
* Install the given bundle into the application.
*
* @param string $bundle
* @return void
*/
public function install($bundle);
}

View File

@@ -0,0 +1,90 @@
<?php namespace Laravel\CLI\Tasks\Bundle;
use Laravel\Bundle;
use FilesystemIterator;
class Publisher {
/**
* Publish a bundle's assets to the public directory.
*
* @param string $bundle
* @return void
*/
public function publish($bundle)
{
$this->move($bundle, $this->from($bundle), $this->to($bundle));
echo "Assets published for bundle [$bundle].".PHP_EOL;
}
/**
* Copy the contents of a bundle's assets to the public folder.
*
* @param string $bundle
* @param string $source
* @param string $destination
* @return void
*/
protected function move($bundle, $source, $destination)
{
if ( ! is_dir($source)) return;
// First we need to create the destination directory if it doesn't
// already exists. This directory hosts all of the assets we copy
// from the installed bundle's source directory.
if ( ! is_dir($destination))
{
mkdir($destination);
}
$items = new FilesystemIterator($source, FilesystemIterator::SKIP_DOTS);
foreach ($items as $item)
{
// If the file system item is a directory, we will recurse the
// function, passing in the item directory. To get the proper
// destination path, we'll replace the root bundle asset
// directory with the root public asset directory.
if ($item->isDir())
{
$path = $item->getRealPath();
$recurse = str_replace($this->from($bundle), $this->to($bundle), $path);
$this->move($bundle, $path, $recurse);
}
// If the file system item is an actual file, we can copy the
// file from the bundle asset directory to the public asset
// directory. The "copy" method will overwrite any existing
// files with the same name.
else
{
copy($item->getRealPath(), $destination.DS.$item->getBasename());
}
}
}
/**
* Get the "to" location of the bundle's assets.
*
* @param string $bundle
* @return string
*/
protected function to($bundle)
{
return PUBLIC_PATH.'bundles'.DS.$bundle.DS;
}
/**
* Get the "from" location of the bundle's assets.
*
* @param string $bundle
* @return string
*/
protected function from($bundle)
{
return Bundle::path($bundle).'public';
}
}

View File

@@ -0,0 +1,29 @@
<?php namespace Laravel\CLI\Tasks\Bundle;
class Repository {
/**
* The root of the Laravel bundle API.
*
* @var string
*/
protected $api = 'http://bundles.laravel.com/api/';
/**
* Get the decoded JSON information for a bundle.
*
* @param string|int $bundle
* @return array
*/
public function get($bundle)
{
// The Bundle API will return a JSON string that we can decode and
// pass back to the consumer. The decoded array will contain info
// regarding the bundle's provider and location, as well as all
// of the bundle's dependencies.
$bundle = @file_get_contents($this->api.$bundle);
return json_decode($bundle, true);
}
}