Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
scrs_zdenek committed Jan 27, 2025
1 parent 845c78d commit 90780d7
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 47 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=8.1 <8.4",
"php": ">=8.3 <9",
"latte/latte": "^3.0",
"nette/robot-loader": "^3.2",
"nette/http": "^3.0"
Expand Down
61 changes: 31 additions & 30 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Easy configuration for single-page sites.
[![Coverage Status](https://coveralls.io/repos/github/drago-ex/simple/badge.svg?branch=master)](https://coveralls.io/github/drago-ex/simple?branch=master)

## Technology
- PHP 8.1 or higher
- PHP 8.3 or higher
- composer

## Knowledge
Expand All @@ -23,112 +23,113 @@ Easy configuration for single-page sites.
composer require drago/simple
```

## Trait Session
# Traits
## Session Trait
```php
use Drago\Simple\Base\Session
use Drago\Simple\Base\Session;

// Get session.
// Get session instance
$this->session();
```

## Trait Message
## Message Trait
```php
use Drago\Simple\Base\Message;

// Save message.
// Save message to session
$this->flashMessage('Message...');

// Print message.
// Retrieve flash message from session
$this->getFlashMessage();
```

## Trait Response
## Response Trait
```php
use Drago\Simple\Base\Response;

// Redirect to a URL
$this->redirect('#');
```

## Controller
## Controller Example
```php
final class Home
{
private Latte\Engine $latte;
private Latte\Engine $latte;


public function __construct(Latte\Engine $latte)
{
$this->latte = $latte;
}
public function __construct(Latte\Engine $latte)
{
$this->latte = $latte;
}
}
```

## Template render
## Template Rendering
```php
public function render(): void
{
$this->latte->render(__DIR__ . '/path/to/dir/template.latte');
$this->latte->render(__DIR__ . '/path/to/dir/template.latte');
}
```

## Template passing parameters
## Passing Parameters to Templates
```php
public function render(): void
{
$this-flashMessage('message...');
$message['message'] = $this->getFlashMessage();
$this->latte->render(__DIR__ . '/path/to/dir/template.latte', $message);
$this->flashMessage('message...');
$message['message'] = $this->getFlashMessage();
$this->latte->render(__DIR__ . '/path/to/dir/template.latte', $message);
}
```

## Template print message
## Template: Print Message
```latte
<p n:if="$message">{$message}</p>
```

## Template default parameter for include files
## Template: Default Parameter for Include Files
```latte
{$basePath}
```

## Forms
Install via composer.
Install Nette Forms via Composer:
```
composer require nette/forms
```

Forms latte macro.
## Forms Latte Macro
```php
$latte->onCompile[] = function () use ($latte) {
FormMacros::install($latte->getCompiler());
};
```

## Translator
Install via composer.
Install the Translator via Composer:
```
composer require drago-ex/translator
```

Translator property.
## Translator Property
```php
private array $lang = ['en', 'cs'];
```

Translator language detect.
## Translator Language Detection
```php
$translator = new Translator(__DIR__ . '/locale');
$translator->setTranslate((new RequestFactory())->fromGlobals()
->detectLanguage($this->lang)
);
```

Translator latte filter.
## Translator Latte Filter
```php
$latte->addFilter('translate', function ($message) use ($translator) {
return $translator->translate($message);
});
```

## Prepared package for simple project
## Prepared Package for Simple Project
[https://github.com/drago-ex/simple-project](https://github.com/drago-ex/simple-project)
11 changes: 7 additions & 4 deletions src/Simple/Base/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


/**
* Flash message.
* Flash message trait for managing session-based messages.
*/
trait Message
{
Expand All @@ -22,6 +22,9 @@ trait Message
private string $fm = 'fm';


/**
* Retrieves the flash message session section.
*/
private function fmId(): SessionSection|Section
{
return $this->session()
Expand All @@ -30,19 +33,19 @@ private function fmId(): SessionSection|Section


/**
* Save the message.
* Saves the flash message to the session.
*/
public function flashMessage(string $message): SessionSection
{
$session = $this->fmId();
$session->message = $message;
$session->setExpiration('5 second');
$session->setExpiration('5 seconds');
return $session;
}


/**
* Send message to template.
* Retrieves the flash message for the template.
*/
public function getFlashMessage(): ?string
{
Expand Down
4 changes: 2 additions & 2 deletions src/Simple/Base/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@


/**
* Http response.
* Trait for handling HTTP responses.
*/
trait Response
{
/**
* Redirects to a new URL.
* Redirects to a specified URL with an optional HTTP status code.
*/
public function redirect(string $url, int $code = Http\IResponse::S302_Found): void
{
Expand Down
3 changes: 2 additions & 1 deletion src/Simple/Base/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@


/**
* @property string $message
* Represents a session section for flash messages.
* @property string $message The flash message stored in the session.
*/
class Section
{
Expand Down
5 changes: 4 additions & 1 deletion src/Simple/Base/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@


/**
* Provides access to session sections as well as session settings and management methods.
* Trait for managing session sections and settings.
*/
trait Session
{
/**
* Returns the session instance.
*/
public function session(): Http\Session
{
$request = (new Http\RequestFactory)->fromGlobals();
Expand Down
43 changes: 35 additions & 8 deletions src/Simple/Latte.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,53 @@


/**
* Templating engine Latte.
* Custom Latte engine with base path calculation.
* Extends Latte\Engine to add base path handling and optimize performance.
* Optimized for PHP 8.3 features like constructor property promotion and readonly properties.
*/
class Latte extends Engine
{
/** @readonly */
private string $basePath;

private RequestFactory $requestFactory;


/**
* Initializes the engine with the RequestFactory instance.
*/
public function __construct(RequestFactory $requestFactory)
{
parent::__construct();
$this->requestFactory = $requestFactory;
$this->basePath = $this->calculateBasePath();
}


/**
* Calculates and returns the base path (without trailing slashes).
*/
private function calculateBasePath(): string
{
return rtrim($this->requestFactory->fromGlobals()->url->basePath, '/');
}


/**
* Base path in template.
* Returns the cached base path.
*/
private function basePath(): string
public function getBasePath(): string
{
return rtrim((new RequestFactory)
->fromGlobals()->url->basePath, '/');
return $this->basePath;
}


/**
* Creates template object.
* Creates a template with the base path and additional parameters.
*/
public function createTemplate(string $name, array $params = []): Template
public function createTemplate(string $name, array $params = [], $clearCache = true): Template
{
$parameters = $params + ['basePath' => $this->basePath()];
$parameters = $params + ['basePath' => $this->getBasePath()];
return parent::createTemplate($name, $parameters);
}
}

0 comments on commit 90780d7

Please sign in to comment.