Skip to content

Commit

Permalink
Merge pull request #15 from Vinelab/fix/#14
Browse files Browse the repository at this point in the history
Fix/#14
  • Loading branch information
Mulkave committed Sep 8, 2014
2 parents 9575d26 + 658c41e commit 713ef6a
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 165 deletions.
12 changes: 8 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

# CDN Assets Manager

[![Latest Stable Version](https://poser.pugx.org/vinelab/cdn/v/stable.svg)](https://packagist.org/packages/vinelab/cdn)
[![Latest Unstable Version](https://poser.pugx.org/vinelab/cdn/v/unstable.svg)](https://packagist.org/packages/vinelab/cdn)
[![Total Downloads](https://poser.pugx.org/vinelab/cdn/downloads.svg)](https://packagist.org/packages/vinelab/cdn)
[![License](https://poser.pugx.org/vinelab/cdn/license.svg)](https://packagist.org/packages/vinelab/cdn)
[![Build Status](https://travis-ci.org/thephpleague/statsd.png?branch=master)](https://travis-ci.org/Vinelab/cdn)
[![Total Downloads](https://poser.pugx.org/league/statsd/downloads.png)](https://packagist.org/packages/vinelab/cdn)

Content Delivery Network Package for Laravel 4

Upload static assets of your choice to a CDN and have the file paths replaced with full URLs.
Expand Down Expand Up @@ -148,13 +152,13 @@ Since the service provider of this package aliases itself as the facade `Cdn` yo

```blade
{{Cdn::asset('public/index.php')}}
// https://s3.amazonaws.com/my-default-bucket/public/index.php
// https://default-bucket.s3.amazonaws.com/public/index.php
{{Cdn::asset('public/assets/js/main.js')}}
// https://s3.amazonaws.com/js-bucket/public/assets/js/main.js
// https://js-bucket.s3.amazonaws.com/public/assets/js/main.js
{{Cdn::asset('public/assets/css/main.css')}}
// https://s3.amazonaws.com/css-bucket/public/assets/css/main.css
// https://css-bucket.s3.amazonaws.com/public/assets/css/main.css
```

## Contributing
Expand Down
13 changes: 7 additions & 6 deletions src/Vinelab/Cdn/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Asset implements AssetInterface{
*/
private $default_include = [
'directories' => ['public'],
'extensions' => [''],
'patterns' => [''],
'extensions' => [],
'patterns' => [],
];

/**
Expand All @@ -30,10 +30,10 @@ class Asset implements AssetInterface{
* @var array
*/
private $default_exclude = [
'directories' => [''],
'files' => [''],
'extensions' => [''],
'patterns' => [''],
'directories' => [],
'files' => [],
'extensions' => [],
'patterns' => [],
'hidden' => true,
];

Expand Down Expand Up @@ -117,6 +117,7 @@ public function parseAndFillConfiguration($configurations)
{
$this->default_include = isset($configurations['include']) ?
array_merge($this->default_include, $configurations['include']) : $this->default_include;

$this->default_exclude = isset($configurations['exclude']) ?
array_merge($this->default_exclude, $configurations['exclude']) : $this->default_exclude;
}
Expand Down
12 changes: 11 additions & 1 deletion src/Vinelab/Cdn/CdnHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public function validate($configuration, $required)

}


/**
* Take url as string and return it parsed object
*
* @param $url
*
* @return mixed
*/
public function parseUrl($url)
{
return parse_url($url);
}

}
2 changes: 2 additions & 0 deletions src/Vinelab/Cdn/Contracts/CdnHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public function getConfigurations();

public function validate($configuration, $required);

public function parseUrl($url);

}
10 changes: 7 additions & 3 deletions src/Vinelab/Cdn/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ private function excludeThis(AssetInterface $asset_holder)
$this->notName($name);
}

// exclude files with this extensions
foreach ($asset_holder->getExcludedExtensions() as $extension) {
$this->notName('*' . $extension);
// exclude files (if exist) with this extensions
$excluded_extensions = $asset_holder->getExcludedExtensions();
if ( ! empty($excluded_extensions))
{
foreach ($asset_holder->getExcludedExtensions() as $extension) {
$this->notName('*' . $extension);
}
}

// exclude the regex pattern
Expand Down
163 changes: 44 additions & 119 deletions src/Vinelab/Cdn/Providers/AwsS3Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class AwsS3Provider extends Provider implements ProviderInterface{
* @var array
*/
protected $default = [
'protocol' => 'https',
'domain' => null,
'url' => null,
'threshold' => 10,
'providers' => [
'aws' => [
Expand All @@ -49,53 +48,14 @@ class AwsS3Provider extends Provider implements ProviderInterface{
*
* @var array
*/
protected $rules = ['key', 'secret', 'buckets', 'domain'];
protected $rules = ['key', 'secret', 'buckets', 'url'];

/**
* @var string
*/
protected $domain;

/**
* @var string
*/
protected $protocol;

/**
* @var string
*/
protected $url;

/**
* @var string
*/
protected $key;

/**
* @var string
*/
protected $secret;

/**
* the type of permission on the files on the CDN
* this array holds the parsed configuration to be used across the class
*
* @var string
* @var Array
*/
protected $acl;

/**
* @var integer
*/
protected $threshold;

/**
* @var array
*/
protected $buckets;
/**
* @var boolean
*/
protected $multiple_buckets;
protected $supplier;

/**
* @var Instance of Aws\S3\S3Client
Expand All @@ -108,7 +68,7 @@ class AwsS3Provider extends Provider implements ProviderInterface{
protected $batch;

/**
* @var Vinelab\Cdn\Contracts\CdnHelperInterface
* @var \Vinelab\Cdn\Contracts\CdnHelperInterface
*/
protected $cdn_helper;

Expand All @@ -133,79 +93,46 @@ public function __construct(
}

/**
* Assign configurations to the properties and return itself
* Read the configuration and prepare an array with the relevant configurations
* for the (AWS S3) provider. and return itself
*
* @param $configurations
*
* @return $this
*/
public function init($configurations)
{
$supplier = $this->parse($configurations);

$this->domain = $supplier['domain'];
$this->protocol = $supplier['protocol'];
$this->url = $supplier['url'];
$this->key = $supplier['key'];
$this->secret = $supplier['secret'];
$this->acl = $supplier['acl'];
$this->threshold = $supplier['threshold'];
$this->buckets = $supplier['buckets'];

return $this;
}

/**
* Read the configuration and prepare an array with the relevant configurations
* for the (AWS S3) provider.
*
* @param $configurations
*
* @return array
*/
private function parse($configurations)
{
// merge the received config array with the default configurations array to
// fill missed keys with null or default values.
$this->default = array_merge($this->default, $configurations);

// TODO: to be removed to a function of common configurations between call providers
$threshold = $this->default['threshold'];
$protocol = $this->default['protocol'];
$domain = $this->default['domain'];

// aws s3 specific configurations
$key = $this->default['providers']['aws']['s3']['credentials']['key'];
$secret = $this->default['providers']['aws']['s3']['credentials']['secret'];
$buckets = $this->default['providers']['aws']['s3']['buckets'];
$acl = $this->default['providers']['aws']['s3']['acl'];

$supplier = [
'domain' => $domain,
'protocol' => $protocol,
'url' => $protocol . '://' . $domain, // compose the url from the protocol and the domain
'key' => $key,
'secret' => $secret,
'acl' => $acl,
'threshold' => $threshold,
'buckets' => $buckets,
'provider_url' => $this->default['url'],
'threshold' => $this->default['threshold'],
'credential_key' => $this->default['providers']['aws']['s3']['credentials']['key'],
'credential_secret' => $this->default['providers']['aws']['s3']['credentials']['secret'],
'buckets' => $this->default['providers']['aws']['s3']['buckets'],
'acl' => $this->default['providers']['aws']['s3']['acl'],
];

// check if any required configuration is missed
$this->configurations->validate($supplier, $this->rules);

return $supplier;
$this->supplier = $supplier;

return $this;
}


/**
* Create a cdn instance and create a batch builder instance
*/
private function connect()
{
// Instantiate an S3 client
$this->s3_client = S3Client::factory( array(
'key' => $this->key,
'secret' => $this->secret,
'key' => $this->credential_key,
'secret' => $this->credential_secret,
)
);

Expand All @@ -218,6 +145,8 @@ private function connect()

/**
* Upload assets
*
* @param $assets
*/
public function upload($assets)
{
Expand All @@ -233,8 +162,8 @@ public function upload($assets)
try {
$this->batch->add($this->s3_client->getCommand('PutObject', [

'Bucket' => key($this->buckets), // the bucket name
'Key' => $file->GetPathName(), // the path of the file on the server (CDN)
'Bucket' => $this->getBucket(), // the bucket name
'Key' => $file->getPathName(), // the path of the file on the server (CDN)
'Body' => fopen($file->getRealpath(), 'r'), // the path of the path locally
'ACL' => $this->acl, // the permission of the file

Expand Down Expand Up @@ -269,48 +198,44 @@ public function upload($assets)
*/
public function urlGenerator($path)
{
return $this->getProtocol() . key($this->getBuckets()) . '.' . $this->getDomain() . $path;
}
$url = $this->cdn_helper->parseUrl($this->getUrl());

/**
* @return string
*/
public function getDomain()
{
return rtrim($this->domain, "/") . '/';
}

/**
* @return string
*/
public function getProtocol()
{
// make sure every protocol is formatted correctly (xxx://)
return rtrim(rtrim($this->protocol, "/"), ":") . '://';
return $url['scheme'] . '://' . $this->getBucket() . '.' . $url['host'] . '/' . $path;
}

/**
* @return string
*/
public function getUrl()
{
return $this->url;
return rtrim($this->provider_url, "/") . '/';
}


/**
* @param string $url
* @return array
*/
public function setUrl($url)
public function getBucket()
{
$this->url = $url;
// this step is very important, "always assign returned array from
// magical function to a local variable if you need to modify it's
// state or apply any php function on it." because the returned is
// a copy of the original variable. this prevent this error:
// Indirect modification of overloaded property
// Vinelab\Cdn\Providers\AwsS3Provider::$buckets has no effect
$bucket = $this->buckets;

return rtrim(key($bucket), "/");
}

/**
* @return array
* @param $attr
*
* @return Mix | null
*/
public function getBuckets()
public function __get($attr)
{
return $this->buckets;
return isset($this->supplier[$attr]) ? $this->supplier[$attr] : null;
}

}
6 changes: 1 addition & 5 deletions src/Vinelab/Cdn/Providers/Contracts/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ public function upload($assets);

public function urlGenerator($path);

public function getDomain();

public function getProtocol();

public function getUrl();

public function getBuckets();
public function getBucket();

}
Loading

0 comments on commit 713ef6a

Please sign in to comment.