Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
* [R] изменен нэймспейс на `AJUR\Template`
* [*] добавлены возвращаемые типы `void` для ряда методов
  • Loading branch information
KarelWintersky committed Feb 5, 2024
1 parent 5fc2679 commit 2c7171a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 32 deletions.
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto
* eol=lf

.git export-ignore
.gitattributes export-ignore
.gitignore export-ignore
tests export-ignore
vendor export-ignore
makefile export-ignore
phpunit.xml export-ignore
test*.* export-ignore
60 changes: 60 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Todo

## Регистрация Smarty-плагинов

Хорошо бы дополнить этот механизм возможность регистрировать плагином произвольную функцию произвольного класса:

```php
Expand All @@ -13,6 +15,8 @@

- добавить обработку параметра `@smarty_plugin_name` - имя, под которым регистрируем метод. По умолчанию совпадает с именем функции.

## Flash-messages

Добавить flash-messages, см как сделано в `slim/flash`

`->setFlashMessage($code, $message)`
Expand All @@ -21,3 +25,59 @@

`->assingFlashMessages($value = 'flash_messages')`

Вердикт: отказались, потому что этот механизм не имеет отношения к шаблонизатору и обертке над ним.

## Механизм Breadcrumbs

Имеет смысл сделать через Stack. pushBreadCumbs($url, $title) - потому что хлебные крошки у нас имеют вид:
```html
<a href="/">Сайт</a>
<a href="/articles/">Статьи</a>
<a href="/articles/1234/">Конкретная статья</a>
```

То есть класть в стек нужно: url + title

```php
require_once __DIR__ . '/vendor/autoload.php';

$s = new \Arris\Core\Stack();

$s->push([
'url' => '/',
'title' => 'Главная страница'
]);

$s->push([
'url' => '/articles/',
'title' => 'Все статьи'
]);

$s->push([
'url' => '/articles/123/',
'title' => 'Моя статья'
]);

$s->reverse();

$string = "\n\n";
do {
$el = $s->pop();

$string .= <<<STR
<a href="{$el['url']}">{$el['title']}</a>\n
STR;
} while (!$s->isEmpty());

var_dump($string);
```

Результат:
```html
<a href="/">Главная страница</a>
<a href="/articles/">Все статьи</a>
<a href="/articles/123/">Моя статья</a>
```
just as planned


7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
"license": "MIT",
"autoload": {
"psr-4": {
"AJUR\\": "src/"
"AJUR\\Template\\": "src/"
}
},
"authors": [
{
"name": "Karel Wintersky",
"email": "[email protected]"
}
]
],
"require-dev": {
"rector/rector": "^0.19.8"
}
}
29 changes: 12 additions & 17 deletions src/Template.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

namespace AJUR;
namespace AJUR\Template;

use JsonException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Smarty;
use SmartyException;

class Template implements TemplateInterface
{
Expand Down Expand Up @@ -36,8 +34,6 @@ class Template implements TemplateInterface

private string $template_file = '';

private array $JSON = [];

public \stdClass $options;

public array $redirect = [
Expand Down Expand Up @@ -90,7 +86,7 @@ public function getSmartyInstance():Smarty
return $this->smarty;
}

public function setRenderType(string $type)
public function setRenderType(string $type): void
{
$this->render_type = $type;
}
Expand All @@ -100,7 +96,7 @@ public function request($key, string $default = '')
return array_key_exists($key, $this->REQUEST) ? $this->REQUEST[$key] : $default;
}

public function assign($key, $value = null)
public function assign($key, $value = null): void
{
if (is_array($key)) {
foreach ($key as $k => $v) {
Expand All @@ -111,7 +107,7 @@ public function assign($key, $value = null)
}
}

public function setRedirect(string $uri = '/', int $code = 200)
public function setRedirect(string $uri = '/', int $code = 200): void
{
$this->force_redirect = $uri;
$this->force_redirect_code = $code;
Expand Down Expand Up @@ -147,19 +143,19 @@ public function makeRedirect(string $uri = null, int $code = null, bool $replace
exit(0);
}

$scheme = (TemplateHelper::is_ssl() ? "https://" : "http://");
$scheme = TemplateHelper::is_ssl() ? "https://" : "http://";
$scheme = str_replace('://', '', $scheme);

header("Location: {$scheme}://{$_SERVER['HTTP_HOST']}{$_uri}", $replace_headers, $_code);
exit(0);
}

public function setTemplate($filename = null)
public function setTemplate($filename = null): void
{
$this->template_file = empty($filename) ? '' : $filename;
}

public function sendHeader(string $type = '')
public function sendHeader(string $type = ''): void
{
$content_type
= empty($type)
Expand Down Expand Up @@ -188,14 +184,14 @@ public function clean($clear_cache = true): bool
return true;
}

public function assignJSON(array $json)
public function assignJSON(array $json): void
{
foreach ($json as $key => $value) {
$this->assign($key, $value);
}
}

public function assignRAW(string $html)
public function assignRAW(string $html): void
{
$this->raw_content = $html;
$this->setRenderType(TemplateInterface::CONTENT_TYPE_RAW);
Expand Down Expand Up @@ -250,12 +246,11 @@ public function render($send_header = false, $clean = false)
}


public function addTitle(string $title_part)
public function addTitle(string $title_part): void
{
$this->titles[] = $title_part;
}


public function makeTitle(string $separator = " ", bool $sort = true, bool $reverse_order = false, bool $clean_extra_spaces = true):string
{
$t = $this->titles;
Expand All @@ -276,6 +271,6 @@ public function makeTitle(string $separator = " ", bool $sort = true, bool $reve

return $t;
}
}


}
# -eof-
5 changes: 3 additions & 2 deletions src/TemplateHelper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AJUR;
namespace AJUR\Template;

class TemplateHelper
{
Expand Down Expand Up @@ -97,5 +97,6 @@ public static function getAllowedValue( $data, $allowed_values_array, $default_v
: $default_value;
}
}
}

}
# -eof-
7 changes: 5 additions & 2 deletions src/TemplateInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AJUR;
namespace AJUR\Template;

use JsonException;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -63,6 +63,7 @@ public function setRenderType(string $type);

/**
* Отдает элемент из массива $_REQUEST
* (если он передан при инициализации)
*
* @param $key
* @param string $default
Expand Down Expand Up @@ -194,4 +195,6 @@ public function getAssignedVars($varName = null);
* @return string|null
*/
public function render($send_header = false, $clean = false);
}
}

# -eof-
14 changes: 5 additions & 9 deletions src/TemplatePlugins.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AJUR;
namespace AJUR\Template;

use ReflectionClass;
use ReflectionException;
Expand Down Expand Up @@ -33,7 +33,7 @@ class TemplatePlugins
* @throws ReflectionException
* @throws SmartyException
*/
public static function register(Smarty $smarty, $plugins = [])
public static function register(Smarty $smarty, $plugins = []): void
{
foreach ($plugins as $entity) {
if (in_array($entity, self::$already_registred)) {
Expand Down Expand Up @@ -122,7 +122,7 @@ public static function size_format(int $size, array $params):string
*
* @return void
*/
public static function dd()
public static function dd(): void
{
if (php_sapi_name() !== "cli") echo '<pre>';
if (func_num_args()) {
Expand Down Expand Up @@ -189,10 +189,6 @@ public static function pluralForm($number, $forms):string
);
}

}






}
# -eof-

0 comments on commit 2c7171a

Please sign in to comment.