-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c5893c
commit ce77ed8
Showing
6 changed files
with
39 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,39 @@ | ||
<?php | ||
|
||
namespace GrapesTheme\Middleware; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Log\LoggerInterface; | ||
namespace Essence\Utility; | ||
|
||
/** | ||
* A middleware that logs each requested URL. | ||
* An utility class to compile templates. | ||
*/ | ||
class AccessLog | ||
{ | ||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
class Template { | ||
|
||
/** | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
/** | ||
* A regex to identify variables in a template. | ||
* | ||
* @var string | ||
*/ | ||
const variablePattern = '~\{?:(?<variable>[a-z0-9_]+)\}?~i'; | ||
|
||
/** | ||
* Executes the middleware. | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param ResponseInterface $response | ||
* @param callable $next | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function __invoke( | ||
ServerRequestInterface $request, | ||
ResponseInterface $response, | ||
callable $next | ||
) { | ||
$response = $next($request, $response); | ||
$path = $request->getUri()->getPath(); | ||
$date = date('c'); | ||
/** | ||
* Compiles a template with variables. | ||
* | ||
* @param string $template Template. | ||
* @param array $variables Variables. | ||
* @return string Compiled template. | ||
*/ | ||
public static function compile($template, array $variables) { | ||
$compile = function ($matches) use ($variables) { | ||
$name = $matches['variable']; | ||
|
||
$this->logger->info("$date: Accessed $path"); | ||
return isset($variables[$name]) | ||
? $variables[$name] | ||
: ''; | ||
}; | ||
|
||
return $response; | ||
} | ||
return preg_replace_callback( | ||
self::variablePattern, | ||
$compile, | ||
$template | ||
); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,6 @@ | |
} | ||
|
||
.Article-body { | ||
@include mixin(); | ||
padding: 1.5rem; | ||
} |