Skip to content

Commit c4e493a

Browse files
Merge pull request #25 from dutchenkoOleg/master
Readme && Laravel troubleshooting
2 parents fdaf7bc + 903b53b commit c4e493a

File tree

2 files changed

+202
-17
lines changed

2 files changed

+202
-17
lines changed

README.md

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,187 @@
99
1010
<img src="https://raw.githubusercontent.com/dutchenkoOleg/storage/master/img/r2d2/r2d2.gif" alt>
1111

12-
----
12+
---
13+
14+
### *Table of contents*
15+
16+
- [Install](#install)
17+
- [Setup](#setup)
18+
- [API](#api)
19+
- [eject](#eject)
20+
- [instance::set](#instanceset)
21+
- [instance::fileUrl](#instancefileurl)
22+
- [instance::fileContent](#instancefilecontent)
23+
24+
---
25+
26+
27+
28+
# Install
29+
30+
```bash
31+
composer require wezom-agency/browserizr
32+
```
33+
34+
---
35+
36+
# Setup
37+
38+
in your core app file:
39+
40+
```php
41+
use WezomAgency\R2D2;
42+
43+
R2D2::eject()->set('KEY', VALUE);
44+
45+
```
46+
47+
List of settings:
48+
49+
- `debug: bool = false`
50+
- `host: string = ''`
51+
- `protocol: string = 'http://'`
52+
- `rootPath: string = ''`
53+
- `resourceRelativePath: string = ''` - _"shortcut"_ to your resources/assets directory
54+
- `svgSpritemapPath: string = ''` - default path to svg sprite map file
55+
56+
57+
Example of settings
58+
59+
```php
60+
use WezomAgency\R2D2;
61+
62+
R2D2::eject()
63+
->set(debug, true)
64+
->set('host', $_SERVER['HTTP_HOST'])
65+
->set('protocol', 'https://')
66+
->set('rootPath', './')
67+
->set('resourceRelativePath', '/my/path/to/resources/')
68+
->set(
69+
'svgSpritemapPath',
70+
R2D2::eject()->resourceUrl('assets/svg/common.svg', true)
71+
);
72+
```
73+
74+
---
75+
76+
# API
77+
78+
## eject
79+
80+
```php
81+
R2D2::eject() instance
82+
```
83+
84+
Ejecting R2D2 instance.
85+
Below a list of instance methods
86+
87+
88+
---
89+
90+
### instance::set
91+
92+
```php
93+
R2D2::eject()->set($key, $value) instance
94+
```
95+
96+
_Parameters:_
97+
98+
| Name | Data type | Default value | Description |
99+
| :--------- | :-------- | :------------ | :------------- |
100+
| **$name** | _string_ | | settings name |
101+
| **$value** | _any_ | | settings value |
102+
103+
List of available settings and their values, see above ([#setup section](#setup))
104+
105+
106+
107+
---
108+
109+
### instance::fileUrl
110+
111+
Generate file url.
112+
113+
```php
114+
R2D2::eject()->fileUrl($url, $timestamp = false, $absolute = false) string
115+
```
116+
117+
118+
_Parameters:_
119+
120+
| Name | Data type | Default value | Description |
121+
| :------------- | :-------- | :------------ | :------------- |
122+
| **$url** | _string_ | | file relative url, from site root, e.q `/assets/css/my.css` |
123+
| **$timestamp** | _booleab_ | `false` | set file versioning with query pair `?time=xxx`, if file doesn't exist - query will be empty string |
124+
| **$absolute** | _booleab_ | `false` | generate absolute url, with `protocol` and `host` setting (see [#setup section](#setup)) |
125+
126+
_Returns:_ URL
127+
128+
129+
#### Usage example
130+
131+
```php
132+
<?php
133+
134+
use WezomAgency\R2D2;
135+
136+
// in core app file:
137+
// R2D2::eject()
138+
// ->set('host', 'my-site.com')
139+
// ->set('protocol', 'https://')
140+
141+
?>
142+
<link rel="stylesheet" href="<?= R2D2::eject()->fileUrl('/assets/css/style1.css'); ?>">
143+
<link rel="stylesheet" href="<?= R2D2::eject()->fileUrl('/assets/css/style2.css', true); ?>">
144+
<link rel="stylesheet" href="<?= R2D2::eject()->fileUrl('/assets/css/style3.css', true, true); ?>">
145+
```
146+
#### Result
147+
148+
```html
149+
<link rel="stylesheet" href="/assets/css/style1.css">
150+
<link rel="stylesheet" href="/assets/css/style2.css?time=1545054627">
151+
<link rel="stylesheet" href="https://my-site.com/assets/css/style3.css?time=1545054627">
152+
```
153+
154+
155+
---
156+
157+
### instance::fileContent
158+
159+
Get file content.
160+
161+
```php
162+
R2D2::eject()->fileContent($path) string
163+
```
164+
165+
_Parameters:_
166+
167+
| Name | Data type | Default value | Description |
168+
| :------------- | :-------- | :------------ | :------------- |
169+
| **$path** | _string_ | | file relative url, from the `rootPath` (see [#setup section](#setup)) |
170+
171+
_Returns:_ file content
172+
173+
174+
#### Usage example
175+
176+
```php
177+
<?php
178+
179+
use WezomAgency\R2D2;
180+
181+
// in core app file:
182+
// R2D2::eject()
183+
// ->set('rootPath', './')
184+
185+
?>
186+
<style><?= R2D2::eject()->fileContent('/assets/css/critical.css'); ?></style>
187+
```
188+
#### Result
189+
190+
```html
191+
<style>html{font:14px/1.3em Arial;color:#222}h1{color:red;font-size:2em}.wysiswyg{font-size:16px;line-height:normal}</style>
192+
```
193+
194+
195+

src/WezomAgency/R2D2.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,28 @@ public static function eject()
4040

4141

4242
/**
43-
* @param string $key
43+
* Setup instance settings
44+
* @param string $name
4445
* @param string $value
4546
* @return R2D2 $this
4647
* @throws \Exception
4748
*/
48-
public function set($key, $value)
49+
public function set($name, $value)
4950
{
50-
if (property_exists($this, $key) === false) {
51+
if (property_exists($this, $name) === false) {
5152
if ($this->debug) {
52-
throw new \Exception("Property $key does not exist in class " . __CLASS__);
53+
throw new \Exception("Property $name does not exist in class " . __CLASS__);
5354
} else {
5455
return $this;
5556
}
5657
}
57-
switch ($key) {
58+
switch ($name) {
5859
case 'rootPath':
5960
case 'resourceRelativePath':
60-
$this->$key = rtrim($value, '/') . '/';
61+
$this->$name = rtrim($value, '/') . '/';
6162
break;
6263
default:
63-
$this->$key = $value;
64+
$this->$name = $value;
6465
}
6566

6667
return $this;
@@ -76,13 +77,14 @@ public function set($key, $value)
7677
public function fileUrl($url, $timestamp = false, $absolute = false)
7778
{
7879

79-
$root = $absolutePath ? ($this->protocol . $this->host) : '/';
80+
$root = $absolute ? ($this->protocol . $this->host) : '/';
8081
$file = trim($url, '/');
8182
if ($timestamp) {
82-
if ($this->fileUrlTimestampsCache[$file] === null && is_file($this->rootPath . $file)) {
83+
if (!isset($this->fileUrlTimestampsCache[$file]) && is_file($this->rootPath . $file)) {
8384
$this->fileUrlTimestampsCache[$file] = '?time=' . fileatime($this->rootPath . $file);
8485
}
85-
return $root . $file . $this->fileUrlTimestampsCache[$file];
86+
$query = isset($this->fileUrlTimestampsCache[$file]) ? $this->fileUrlTimestampsCache[$file] : '';
87+
return $root . $file . $query;
8688
}
8789
return $root . $file;
8890
}
@@ -93,8 +95,8 @@ public function fileUrl($url, $timestamp = false, $absolute = false)
9395
*/
9496
public function fileContent($path)
9597
{
96-
$path = $this->fileUrl($path, false, false);
97-
return file_get_contents($this->rootPath . $path);
98+
$file = trim($path, '/');
99+
return file_get_contents($this->rootPath . $file);
98100
}
99101

100102

@@ -252,11 +254,11 @@ public function svgSymbol($id, $attrs = [], $spritemap = null)
252254
*/
253255
protected function getNonRepeatingId ($id, $number = null) {
254256
$key = $id . ($number ? '-' . $number : '');
255-
if ($this->idCache[$key] === null) {
256-
$this->idCache[$key] = 1;
257-
return $key;
257+
if (isset($this->idCache[$key])) {
258+
return $this->getNonRepeatingId($id, ($number ? ++$number : 1));
258259
}
259-
return $this->getNonRepeatingId($id, ($number ? ++$number : 1));
260+
$this->idCache[$key] = 1;
261+
return $key;
260262
}
261263

262264
/**

0 commit comments

Comments
 (0)