Skip to content

Commit 8e28e5f

Browse files
committed
AWS support
1 parent d25a485 commit 8e28e5f

File tree

3 files changed

+118
-93
lines changed

3 files changed

+118
-93
lines changed

README.md

+23-9
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Features:
1212
* Intergrates [Orchestra/Asset](http://orchestraplatform.com/docs/3.0/components/asset) to provide Asset dependencies managment
1313
* Your App & Views remain theme-agnostic. Include new themes with (almost) no modifications
1414

15-
## How it works
15+
# How it works
1616

1717
Very simple, you create a folder for each Theme in 'resources/views' and keep all your views seperated. The same goes for assets: create a folder for each theme in your 'public' directory. Set your active theme and you are done. The rest of your application remains theme-agnostic©, which means that when you `View::make('index')` you will access the `index.blade.php` from your selected theme's folder. Same goes for your assets.
1818

19-
## Installation
19+
# Installation
2020

2121
Edit your project's `composer.json` file to require:
2222

@@ -40,7 +40,7 @@ Almost Done. You only need to publish configuration file to your application wit
4040

4141
That's it. You are now ready to start theming your applications!
4242

43-
## Defining themes
43+
# Defining themes
4444

4545
Simple define your themes in the `themes` array in `config/themes.php`. The format for every theme is very simple:
4646

@@ -67,13 +67,13 @@ Simple define your themes in the `themes` array in `config/themes.php`. The form
6767
```
6868
all settings are optional and can be ommited. Check the example in the configuration file... If you are OK with the defaults then you don't even have to touch the configuration file. If a theme is not found then the default values will be used (Convention over configuration)
6969

70-
## Extending themes
70+
# Extending themes
7171

7272
You can set a theme to extend an other. When you are requesting a view/asset that doesn't exist in your active theme, then it will be resolved from it's parent theme. You can easily create variations of your theme by simply overiding your views/themes that are different.
7373

7474
All themes fall back to the default laravel folders if a resource is not found on the theme folders. So for example you can leave your common libraries (jquery/bootstrap ...) in your `public` folder and use them from all themes. No need to dublicate common assets for each theme!
7575

76-
## Working with Themes
76+
# Working with Themes
7777

7878
The default theme can be configured in the `theme.php` configuration file. Working with themes is very straightforward. Use:
7979

@@ -98,7 +98,7 @@ class themeSelectServiceProvider extends ServiceProvider {
9898
```
9999
...or you can use a middleware to apply a theme to a `Route::group()` etc. For more advanced example check demo application: [Set Theme in Session](https://github.com/igaster/laravel-theme-demo)
100100

101-
## Building your views
101+
# Building your views
102102

103103
Whenever you need the url of a local file (image/css/js etc) you can retrieve its path with:
104104

@@ -120,7 +120,7 @@ Theme::css('file-name')
120120
Theme::img('src','alt', 'class-name')
121121
```
122122

123-
## Assets Managment (Optional)
123+
# Assets Managment (Optional)
124124

125125
This package provides intergration with [Orchestra/Asset](http://orchestraplatform.com/docs/3.0/components/asset) component. All the features are explained in the official documentation. If you don't need the extra functinality you can skip this section. Orchestra/Asset is NOT installed along with this package - you have to install it manualy.
126126

@@ -147,7 +147,7 @@ Please note that you are just defining your css/js files but not actually dumpin
147147

148148
exactly where you want write your declerations.
149149

150-
## Assets dependencies
150+
# Assets dependencies
151151

152152
This is an [Orchestra/Asset](http://orchestraplatform.com/docs/3.0/components/asset) feature explained well in the official documentation. Long story short:
153153

@@ -159,6 +159,20 @@ and your assets dependencies will be auto resolved. Your assets will be exported
159159
@js (jquery.js, jq)
160160
@js (bootstrap.js, bs, jq)
161161

162-
## Important Note:
162+
# Important Note:
163163

164164
Laravel is compiling your views every-time you make an edit. A compiled view will not recompile unless you make any edit to your view. Keep this in mind while you are developing themes...
165+
166+
# FAQ:
167+
168+
##### Is this package compatible with AWS?
169+
Yes with one exception: If you are building Theme hierarcies, asset's will not be looked up on the parent theme. Performing file searching on a remote repository is not the best practice. Should be addressed in a future version... However Blade templates auto-discovery works fine since they are local files.
170+
171+
##### What about external assets (eg CDN)?
172+
Link directly to your external assets. Every url that starts with http(s) will not be proccesed by default.
173+
174+
##### How do I change the public path?
175+
Rebind Laravel's 'path.public'. [(More info)](https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5)
176+
177+
##### I'm editing a view but I dont see the changes
178+
Laravel is compiling your views every-time you make an edit. A compiled view will not recompile unless you make any edit to your view. Keep this in mind while you are developing themes...

src/Theme.php

+28-75
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ class Theme extends Tree\Item {
55
public $assetPath;
66
public $viewsPath;
77

8-
public function __construct($name, $assetPath = null, $viewsPath = null){
9-
$this->name = $name;
10-
$this->assetPath = $assetPath === null ? $name : $assetPath;
11-
$this->viewsPath = $viewsPath === null ? $name : $viewsPath;
8+
public function __construct($themeName, $assetPath = null, $viewsPath = null){
9+
$this->name = $themeName;
10+
$this->assetPath = $assetPath === null ? $themeName : $assetPath;
11+
$this->viewsPath = $viewsPath === null ? $themeName : $viewsPath;
1212
}
1313

1414
public function getParent(){
@@ -18,19 +18,34 @@ public function getParent(){
1818
return null;
1919
}
2020

21+
22+
/**
23+
* Attach theme paths to a local Url. The Url must be a resource located on the asset path
24+
* of the current theme or it's parents.
25+
*
26+
* @param string $url
27+
* @return string
28+
*/
2129
public function url($url){
30+
// return external URLs unmodified
2231
if(preg_match('/^((http(s?):)?\/\/)/i',$url))
2332
return $url;
2433

34+
// Is it on AWS? Dont lookup parent themes...
35+
if(preg_match('/^((http(s?):)?\/\/)/i',$this->assetPath))
36+
return $this->assetPath.'/'.ltrim($url, '/');
37+
38+
// Lookup asset in current's theme asset path
2539
$fullUrl = (empty($this->assetPath) ? '' : '/').$this->assetPath.'/'.ltrim($url, '/');
2640

2741
if (file_exists($fullPath = public_path($fullUrl)))
2842
return $fullUrl;
2943

44+
// If not found then lookup in parent's theme asset path
3045
if ($this->getParent())
3146
return $this->getParent()->url($url);
3247

33-
//Asset not found
48+
// Asset not found at all. Error handling
3449
$action = \Config::get('themes.asset_not_found','LOG_ERROR');
3550

3651
if ($action == 'THROW_EXCEPTION')
@@ -39,7 +54,13 @@ public function url($url){
3954
\Log::warning("Asset not found [$url] in Theme [".\Theme::get()."]");
4055
}
4156

42-
// get theme's configuration
57+
/**
58+
* Return the configuration value of $key for the current theme. Configuration values
59+
* are stored per theme in themes.php config file.
60+
*
61+
* @param string $key
62+
* @return mixed
63+
*/
4364
public function config($key){
4465
if (array_key_exists($key, $conf = \Config::get("themes.themes")[$this->name]))
4566
return $conf[$key];
@@ -51,72 +72,4 @@ public function config($key){
5172
}
5273

5374

54-
}
55-
56-
57-
// use Illuminate\Support\Facades\Config;
58-
59-
// class Theme {
60-
// public $themeName;
61-
// public $assetPath;
62-
// public $viewsPath;
63-
// public $parentTheme;
64-
65-
// public static $defaultViewsPath = '';
66-
67-
// /**
68-
// *
69-
// * @param string $themeName
70-
// * @param array $options : ['assetPath' => xx, 'viewsPath' => xx, 'extends' => xx]
71-
// *
72-
// */
73-
// public function __construct($themeName, $options = []){
74-
75-
// if (empty(static::$defaultViewsPath))
76-
// static::$defaultViewsPath = Config::get('view.paths')[0];
77-
78-
// $defaults = [
79-
// 'asset-path' => $themeName,
80-
// 'views-path' => $themeName,
81-
// 'extends' => '',
82-
// ];
83-
84-
// $options = array_merge($defaults, $options);
85-
86-
// $this->themeName = $themeName;
87-
// $this->assetPath = $options['asset-path'];
88-
// $this->viewsPath = $options['views-path'];
89-
// $this->parentTheme = Themes::get($options['extends']);
90-
// }
91-
92-
// public function url($url){
93-
// if(preg_match('/^((http(s?):)?\/\/)/i',$url))
94-
// return $url;
95-
96-
// $fullUrl = (empty($this->assetPath) ? '' : '/').$this->assetPath.'/'.ltrim($url, '/');
97-
98-
// if (!file_exists($fullPath = base_path('public').$fullUrl))
99-
// if (empty($this->parentTheme))
100-
// throw new \Exception("$fullPath - not found");
101-
// else
102-
// return $this->parentTheme->url($url);
103-
104-
// return $fullUrl;
105-
// }
106-
107-
// public function activate(){
108-
// Config::set('view.paths', $this->pathsList_Views());
109-
// Themes::set($this->themeName);
110-
// }
111-
112-
// public function pathsList_Views(){
113-
// if (!empty($this->parentTheme))
114-
// $paths = $this->parentTheme->pathsList_Views();
115-
// else
116-
// $paths = [];
117-
118-
// array_unshift($paths,static::$defaultViewsPath.'/'.$this->viewsPath);
119-
// return $paths;
120-
// }
121-
122-
// }
75+
}

src/Themes.php

+67-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ public function __construct(){
1616
$this->root = new Theme('root','','');
1717
}
1818

19-
// Add a new theme to the Tree
19+
/**
20+
* Add a new theme to the hierarcy. Optionaly define a parent theme.
21+
*
22+
* @param \igaster\laravelTheme\Theme $theme
23+
* @param string $parentName
24+
* @return \igaster\laravelTheme\Theme
25+
*/
2026
public function add(Theme $theme, $parentName = ''){
2127

2228
if ($parentName)
@@ -28,21 +34,37 @@ public function add(Theme $theme, $parentName = ''){
2834
return $theme;
2935
}
3036

31-
// find a Theme (by name)
32-
public function find($name){
33-
return $this->root->searchChild(function($item) use($name){
34-
if ($item->name == $name)
37+
/**
38+
* Find a Theme (by name)
39+
*
40+
* @param string $themeName
41+
* @return \igaster\laravelTheme\Theme
42+
*/
43+
public function find($themeName){
44+
return $this->root->searchChild(function($item) use($themeName){
45+
if ($item->name == $themeName)
3546
return $item;
3647
else
3748
return false;
3849
});
3950
}
4051

52+
/**
53+
* Check if $themeName is a valid Theme
54+
*
55+
* @param string $themeName
56+
* @return bool
57+
*/
4158
public function exists($themeName){
4259
return ($this->find($themeName)!==false);
4360
}
4461

45-
// Set active theme (by name)
62+
/**
63+
* Set $themeName is the active Theme
64+
*
65+
* @param string $themeName
66+
* @return void
67+
*/
4668
public function set($themeName){
4769
if (!Config::get('themes.enabled', true))
4870
return;
@@ -73,33 +95,69 @@ public function set($themeName){
7395
$themeViewFinder->setPaths($paths);
7496
}
7597

76-
// get active theme (name)
98+
/**
99+
* Get active's Theme Name
100+
*
101+
* @return string
102+
*/
77103
public function get(){
78104
return $this->activeTheme ? $this->activeTheme->name : '';
79105
}
80106

81-
// get current theme's configuration
107+
/**
108+
* Return current theme's configuration option for $key
109+
*
110+
* @param string $key
111+
* @return mixed
112+
*/
82113
public function config($key){
83114
return $this->activeTheme->config($key);
84115
}
85116

117+
/**
118+
* Attach current theme's paths to a local Url. The Url must be a resource located on the asset path
119+
* of the current theme or it's parents.
120+
*
121+
* @param string $url
122+
* @return string
123+
*/
86124
public function url($url){
87125
if (Config::get('themes.enabled', true))
88126
return $this->activeTheme->url($url);
89127
else
90128
return $url;
91129
}
92130

93-
// Helper Functions (for Blade files)
131+
//---------------- Helper Functions (for Blade files) -------------------------
94132

133+
/**
134+
* Return css link for $href
135+
*
136+
* @param string $href
137+
* @return string
138+
*/
95139
public function css($href){
96140
return '<link media="all" type="text/css" rel="stylesheet" href="'.$this->url($href).'">'."\n";
97141
}
98142

143+
/**
144+
* Return script link for $href
145+
*
146+
* @param string $href
147+
* @return string
148+
*/
99149
public function js($href){
100150
return '<script src="'.$this->url($href).'"></script>'."\n";
101151
}
102152

153+
/**
154+
* Return img tag
155+
*
156+
* @param string $src
157+
* @param string $alt
158+
* @param string $Class
159+
* @return string
160+
*/
103161
public function img($src, $alt='', $Class=''){
104162
return '<img src="'.$this->url($src).'" alt="'.$alt.'" class="'.$Class.'">'."\n";
105163
}

0 commit comments

Comments
 (0)