Skip to content

Commit

Permalink
View 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jul 4, 2022
1 parent 8b28440 commit cfcdc7a
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 44 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<a name="2.0.0"></a>
# [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.

<a name="1.0.0"></a>
# [1.0.0](https://github.com/glowyphp/view) (2022-02-05)
* Initial release
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ View Package provides basic methods for creating extendable PHP Views.
</p>

<p align="center">
<a href="https://github.com/glowyphp/view/releases"><img alt="Version" src="https://img.shields.io/github/release/glowyphp/view.svg?label=version&style=for-the-badge"></a>
<a href="https://github.com/glowyphp/view"><img src="https://img.shields.io/badge/license-MIT-blue.svg?style=for-the-badge" alt="License"></a>
<a href="https://packagist.org/packages/glowy/view"><img src="https://poser.pugx.org/glowy/view/downloads?style=for-the-badge" alt="Total downloads"></a><img src="http://poser.pugx.org/glowy/view/require/php?style=for-the-badge">
<img src="https://img.shields.io/badge/license-MIT-blue.svg?label=License" alt="License MIT"> <a href="https://packagist.org/packages/glowy/view"><img src="https://poser.pugx.org/glowy/view/downloads" alt="Total downloads"></a> <img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/glowyphp/view?label=Stars"> <img alt="GitHub forks" src="https://img.shields.io/github/forks/glowyphp/view?label=Forks"> <a href="https://hitsofcode.com"><img alt="Hits of Code" src="https://hitsofcode.com/github/glowyphp/view?branch=2.x"></a> <a href="https://discord.gg/ewQkqgfBAc"><img src="https://img.shields.io/discord/423097982498635778.svg?logo=discord&label=Discord%20Chat" alt="Discord"></a>
</p>


<br>

### Installation
Expand Down
29 changes: 17 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
"authors": [
{
"name": "Sergey Romanenko",
"email": "[email protected]",
"homepage": "https://github.com/Awilum"
"email": "[email protected]",
"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": {
Expand All @@ -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"
}
}
6 changes: 0 additions & 6 deletions phpstan copy.neon

This file was deleted.

69 changes: 50 additions & 19 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@

namespace Glowy\View;

use Stringable;
use ArrayAccess;
use Glowy\Macroable\Macroable;
use BadMethodCallException;
use RuntimeException as ViewException;
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;
Expand All @@ -35,7 +36,7 @@
*
* @template TKey of array-key
*/
class View implements \ArrayAccess
class View implements ArrayAccess, Stringable
{
use Macroable {
__call as macroCall;
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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() ?: '';
Expand Down
8 changes: 5 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/fetch.php
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<?php use function Glowy\View\e ?>
<?= e($this->fetch('foo')) ?>
1 change: 1 addition & 0 deletions tests/fixtures/fetch_first.php
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<?php use function Glowy\View\e ?>
<?= e($this->fetchFirst(['bar', 'foo'])) ?>
1 change: 1 addition & 0 deletions tests/fixtures/fetch_unless.php
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<?php use function Glowy\View\e ?>
<?= e($this->fetchUnless(false, 'foo')) ?>
1 change: 1 addition & 0 deletions tests/fixtures/fetch_when.php
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<?php use function Glowy\View\e ?>
<?= e($this->fetchWhen(true, 'foo')) ?>
3 changes: 3 additions & 0 deletions tests/fixtures/fetch_with_data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php use function Glowy\View\e ?>
<?= e($this->fetch('foo')) ?>
<?= e($foo) ?>
1 change: 1 addition & 0 deletions tests/fixtures/macro.php
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<?php use function Glowy\View\e ?>
<?= e($foo) ?> <?= e($bar) ?>
1 change: 1 addition & 0 deletions tests/fixtures/magic.php
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<?php use function Glowy\View\e ?>
<?= e($this->foo) ?>
1 change: 1 addition & 0 deletions tests/fixtures/share.php
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<?php use function Glowy\View\e ?>
<?= e($share) ?>

0 comments on commit cfcdc7a

Please sign in to comment.