diff --git a/CHANGELOG.md b/CHANGELOG.md
index bfcbf15..aaf2029 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+
+# [2.0.0](https://github.com/glowyphp/view) (2022-07-04)
+* All Helpers functions are placed into the Glowy/View namespace.
+* Use union types.
+* Moving to PHP 8.1
+* Added new public function `file` to set view file.
+* Added new public function `data` to set view data.
+
# [1.0.0](https://github.com/glowyphp/view) (2022-02-05)
* Initial release
diff --git a/README.md b/README.md
index f308372..bf1e80d 100644
--- a/README.md
+++ b/README.md
@@ -4,12 +4,9 @@ View Package provides basic methods for creating extendable PHP Views.
-
-
-
+
-
### Installation
diff --git a/composer.json b/composer.json
index 6d95600..6484bf9 100644
--- a/composer.json
+++ b/composer.json
@@ -12,19 +12,19 @@
"authors": [
{
"name": "Sergey Romanenko",
- "email": "sergey.romanenko@flextype.org",
- "homepage": "https://github.com/Awilum"
+ "email": "awilum@msn.com",
+ "homepage": "https://awilum.github.io"
}
],
"require": {
- "php": "^7.4 || ^8.0",
+ "php": "^8.1",
"ext-json": "*",
"ext-mbstring": "*",
"ext-spl": "*",
"ext-fileinfo": "*",
- "glowy/macroable": "^3.1.0",
- "glowy/strings": "^4.2.0",
- "glowy/filesystem": "^3.0.0"
+ "glowy/macroable": "^4.0.0",
+ "glowy/strings": "^5.0.1",
+ "glowy/filesystem": "^5.0.0"
},
"autoload":{
"psr-4": {
@@ -34,16 +34,21 @@
"src/helpers.php"
]
},
- "require-dev": {
- "doctrine/coding-standard": "9.0.0",
- "pestphp/pest": "^1.21.1",
- "phpstan/phpstan": "^1.4.5",
- "symfony/var-dumper": "^5.4.3"
- },
"config": {
+ "apcu-autoloader": true,
+ "optimize-autoloader": true,
+ "platform-check": false,
+ "sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
+ },
+ "require-dev": {
+ "ext-iconv": "*",
+ "doctrine/coding-standard": "9.0.0",
+ "pestphp/pest": "^1.21.1",
+ "phpstan/phpstan": "^1.8.0",
+ "symfony/var-dumper": "^6.1.0"
}
}
diff --git a/phpstan copy.neon b/phpstan copy.neon
deleted file mode 100644
index 5965df0..0000000
--- a/phpstan copy.neon
+++ /dev/null
@@ -1,6 +0,0 @@
-parameters:
- level: 1
- checkMissingIterableValueType: false
- checkGenericClassInNonGenericObjectType: false
- paths:
- - src
diff --git a/src/View.php b/src/View.php
index 23b9592..2af899b 100644
--- a/src/View.php
+++ b/src/View.php
@@ -4,6 +4,7 @@
namespace Glowy\View;
+use Stringable;
use ArrayAccess;
use Glowy\Macroable\Macroable;
use BadMethodCallException;
@@ -11,19 +12,19 @@
use LogicException as ViewLogicException;
use InvalidArgumentException as ViewInvalidArgumentException;
+use function Glowy\Filesystem\filesystem;
+use function Glowy\Strings\strings;
+use function Glowy\View\view;
use function array_key_exists;
use function array_merge;
use function call_user_func;
use function extract;
-use function filesystem;
use function is_array;
use function is_null;
use function ob_get_clean;
use function ob_start;
use function sprintf;
-use function strings;
use function substr;
-use function view;
use function vsprintf;
use const EXTR_REFS;
@@ -35,7 +36,7 @@
*
* @template TKey of array-key
*/
-class View implements \ArrayAccess
+class View implements ArrayAccess, Stringable
{
use Macroable {
__call as macroCall;
@@ -56,11 +57,11 @@ class View implements \ArrayAccess
protected static string $directory = '';
/**
- * The name of the view.
+ * The view file path.
*
- * @var string View name.
+ * @var string View file path.
*/
- protected string $view;
+ protected string $viewFilePath;
/**
* The array of view data.
@@ -93,7 +94,7 @@ class View implements \ArrayAccess
*
* @var string|null Section name.
*/
- protected ?string $sectionName = null;
+ protected string|null $sectionName = null;
/**
* Set section content mode:
@@ -127,12 +128,32 @@ class View implements \ArrayAccess
/**
* Create a new view instance.
*
- * @param string $view Name of the view file
- * @param array $data Array of view variables
+ * @param string|null $view Name of the view file.
+ * @param array $data Array of view variables.
*
* @throws ViewException
*/
- public function __construct(string $view, array $data = [])
+ public function __construct(string|null $view = null, array $data = [])
+ {
+ if ($view !== null) {
+ $this->file($view);
+ }
+
+ // Set view data
+ $this->data($data);
+
+ // Set view content
+ $this->content = '';
+ }
+
+ /**
+ * Set view file path.
+ *
+ * @param string|null $view Name of the view file.
+ *
+ * @return $this
+ */
+ public function file(string|null $view = null): self
{
$viewFilePath = self::getFilePath($view);
@@ -141,14 +162,10 @@ public function __construct(string $view, array $data = [])
throw new ViewException(vsprintf("%s(): The '%s' view does not exist.", [__METHOD__, $view]));
}
- // Set view file
- $this->view = $viewFilePath;
+ // Set view file path
+ $this->viewFilePath = $viewFilePath;
- // Set view data
- $this->data = $data;
-
- // Set view content
- $this->content = '';
+ return $this;
}
/**
@@ -199,6 +216,20 @@ public function with($key, $value = null)
return $this;
}
+ /**
+ * Set the array of view data.
+ *
+ * @param array $data The array of view data.
+ *
+ * @return $this
+ */
+ public function data(array $data): self
+ {
+ $this->data = $data;
+
+ return $this;
+ }
+
/**
* Get the array of view data.
*
@@ -313,7 +344,7 @@ public function render(?callable $callback = null): string
ob_start();
// Include view file
- include $this->view;
+ include $this->viewFilePath;
// Write content.
$this->content = ob_get_clean() ?: '';
diff --git a/src/helpers.php b/src/helpers.php
index 55fb753..9ce9287 100644
--- a/src/helpers.php
+++ b/src/helpers.php
@@ -2,18 +2,20 @@
declare(strict_types=1);
+namespace Glowy\View;
+
use Glowy\View\View;
if (! function_exists('view')) {
/**
* Create a new view instance.
*
- * @param string $view Name of the view file.
+ * @param string|null $view Name of the view file.
* @param array $data Array of view variables.
*
- * @return View
+ * @return \Glowy\View\View
*/
- function view(string $view, array $data = []): View
+ function view(string|null $view = null, array $data = []): \Glowy\View\View
{
return new View($view, $data);
}
diff --git a/tests/ViewTest.php b/tests/ViewTest.php
index c6c7ec6..a72097e 100644
--- a/tests/ViewTest.php
+++ b/tests/ViewTest.php
@@ -2,6 +2,8 @@
declare(strict_types=1);
+use function Glowy\View\view;
+use function Glowy\View\e;
use Glowy\View\View;
use RuntimeException as ViewException;
use LogicException as ViewLogicException;
@@ -170,6 +172,20 @@
$view->display();
});
+test('data', function (): void {
+ $view = view()->file('fetch')->data(['foo' => 'Foo']);
+
+ $this->expectOutputString("Foo");
+ $view->display();
+});
+
+test('fetch view file with data', function (): void {
+ $view = view()->file('fetch_with_data')->with(['foo' => 'Foo']);
+
+ $this->expectOutputString("FooFoo");
+ $view->display();
+});
+
test('fetch', function (): void {
$view = view('fetch');
diff --git a/tests/fixtures/fetch.php b/tests/fixtures/fetch.php
index 561c342..e2e0963 100644
--- a/tests/fixtures/fetch.php
+++ b/tests/fixtures/fetch.php
@@ -1 +1,2 @@
+
= e($this->fetch('foo')) ?>
\ No newline at end of file
diff --git a/tests/fixtures/fetch_first.php b/tests/fixtures/fetch_first.php
index 4e85495..bcbe509 100644
--- a/tests/fixtures/fetch_first.php
+++ b/tests/fixtures/fetch_first.php
@@ -1 +1,2 @@
+
= e($this->fetchFirst(['bar', 'foo'])) ?>
\ No newline at end of file
diff --git a/tests/fixtures/fetch_unless.php b/tests/fixtures/fetch_unless.php
index 3644514..66c113d 100644
--- a/tests/fixtures/fetch_unless.php
+++ b/tests/fixtures/fetch_unless.php
@@ -1 +1,2 @@
+
= e($this->fetchUnless(false, 'foo')) ?>
\ No newline at end of file
diff --git a/tests/fixtures/fetch_when.php b/tests/fixtures/fetch_when.php
index f2ab053..7834d24 100644
--- a/tests/fixtures/fetch_when.php
+++ b/tests/fixtures/fetch_when.php
@@ -1 +1,2 @@
+
= e($this->fetchWhen(true, 'foo')) ?>
\ No newline at end of file
diff --git a/tests/fixtures/fetch_with_data.php b/tests/fixtures/fetch_with_data.php
new file mode 100644
index 0000000..a4a2811
--- /dev/null
+++ b/tests/fixtures/fetch_with_data.php
@@ -0,0 +1,3 @@
+
+= e($this->fetch('foo')) ?>
+= e($foo) ?>
\ No newline at end of file
diff --git a/tests/fixtures/macro.php b/tests/fixtures/macro.php
index 8ac80a5..ccae648 100644
--- a/tests/fixtures/macro.php
+++ b/tests/fixtures/macro.php
@@ -1 +1,2 @@
+
= e($foo) ?> = e($bar) ?>
\ No newline at end of file
diff --git a/tests/fixtures/magic.php b/tests/fixtures/magic.php
index ca3b85a..34cda6a 100644
--- a/tests/fixtures/magic.php
+++ b/tests/fixtures/magic.php
@@ -1 +1,2 @@
+
= e($this->foo) ?>
\ No newline at end of file
diff --git a/tests/fixtures/share.php b/tests/fixtures/share.php
index 5424118..571ae51 100644
--- a/tests/fixtures/share.php
+++ b/tests/fixtures/share.php
@@ -1 +1,2 @@
+
= e($share) ?>
\ No newline at end of file