Skip to content

Commit

Permalink
Merge pull request #45 from madapaja/fix_mobile_detect
Browse files Browse the repository at this point in the history
Fix mobile detection on runtime
  • Loading branch information
koriym authored Sep 3, 2023
2 parents 01334b9 + 7264080 commit 36c4809
Show file tree
Hide file tree
Showing 33 changed files with 280 additions and 162 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Coding Standards

on:
push:
pull_request:
workflow_dispatch:

jobs:
cs:
uses: ray-di/.github/.github/workflows/coding-standards.yml@v1
with:
php_version: 8.1
13 changes: 13 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Static Analysis

on:
push:
pull_request:
workflow_dispatch:

jobs:
sa:
uses: ray-di/.github/.github/workflows/static-analysis.yml@v1
with:
php_version: 8.1
has_crc_config: true
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Update copyright year(s) in license file
name: Update copyright year in license file

on:
workflow_dispatch:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/composer.lock
/.php_cs.cache
/.phpunit.result.cache
/.phpcs-cache
18 changes: 18 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"symbol-whitelist" : [
"Mobile_Detect"
],
"php-core-extensions" : [
"Core",
"date",
"json",
"hash",
"pcre",
"Phar",
"Reflection",
"SPL",
"random",
"standard"
],
"scan-files" : []
}
26 changes: 14 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@
],
"require": {
"php": "^8.0",
"bear/resource": "^1.15.2",
"bear/resource": "^1.20",
"bear/sunday": "^1.2",
"bear/app-meta": "^1.1",
"bear/app-meta": "^1.8",
"twig/twig": "^2.15.3 || ^v3.4.3",
"mobiledetect/mobiledetectlib": "^2.7",
"mobiledetect/mobiledetectlib": "^2.7 || ^3.74",
"ray/di": "^2.7",
"doctrine/annotations": "^1.13.3"
"ray/aop": "^2.13",
"psr/log": "^3.0"
},
"require-dev": {
"doctrine/annotations": "^1.13.3 || ^2.0",
"phpunit/phpunit": "^9.5.21",
"squizlabs/php_codesniffer": "^3.7",
"phpmd/phpmd": "^2.6",
"rector/rector": "^0.15.3",
"ray/rector-ray": "^1.0",
"doctrine/coding-standard": "^11.1",
"vimeo/psalm": "^5.4"
"doctrine/coding-standard": "^12.0",
"vimeo/psalm": "^5.4",
"phpstan/phpstan": "^1.10"
},
"autoload":{
"psr-4":{
Expand All @@ -46,13 +49,12 @@
}
},
"scripts": {
"test": ["@setup", "vendor/bin/phpunit"],
"test74": ["/usr/local/Cellar/php@7.4/7.4.14/bin/php vendor/bin/phpunit"],
"tests": ["@cs", "phpstan analyse -l max src tests -c phpstan.neon --no-progress", "@test"],
"coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
"cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=phpcs.xml src;"],
"test": ["phpunit"],
"tests": ["@cs", "@sa", "@test"],
"coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage phpunit --coverage-text --coverage-html=build/coverage"],
"cs": ["phpcs --standard=phpcs.xml src"],
"cs-fix": ["phpcbf src tests"],
"sa": "psalm"
"sa": ["psalm", "phpstan"]
},
"config": {
"allow-plugins": {
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: max
paths:
- src
- tests
excludePaths:
- */tests/tmp/*
- */tests/Fake/*
ignoreErrors:
4 changes: 2 additions & 2 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="tests/bootstrap.php">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage> <testsuites>
<testsuite name="TwigModule test suite">
<directory suffix="Test.php">tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
7 changes: 7 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<issueHandlers>
<UndefinedClass>
<errorLevel type="suppress">
<directory name="src/" />
</errorLevel>
</UndefinedClass>
</issueHandlers>
<projectFiles>
<directory name="src" />
<ignoreFiles>
Expand Down
2 changes: 1 addition & 1 deletion src/AppPathProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function get(): array
{
Expand Down
4 changes: 4 additions & 0 deletions src/ErrorPagerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

use function assert;
use function is_array;

class ErrorPagerRenderer implements RenderInterface
{
public function __construct(
Expand All @@ -28,6 +31,7 @@ public function __construct(
*/
public function render(ResourceObject $ro): string
{
assert(is_array($ro->body));
$ro->view = $this->twig->render($this->errorPage, $ro->body);

return $ro->view;
Expand Down
23 changes: 16 additions & 7 deletions src/MobileTemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,41 @@

namespace Madapaja\TwigModule;

use Detection\MobileDetect;
use Madapaja\TwigModule\Annotation\TwigPaths;
use Mobile_Detect;
use Ray\Di\Di\Named;

use function class_alias;
use function class_exists;
use function file_exists;
use function sprintf;
use function str_replace;

class MobileTemplateFinder implements TemplateFinderInterface
{
private TemplateFinder $templateFinder;

/** @param array<string> $paths */
public function __construct(
private mixed $userAgent = '',
#[TwigPaths]
private array $paths = [],
private array $paths,
#[Named('original')]
private TemplateFinderInterface $templateFinder,
) {
$this->templateFinder = new TemplateFinder();
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function __invoke(string $name): string
{
if (! class_exists(MobileDetect::class)) {
// @codeCoverageIgnoreStart
class_alias(Mobile_Detect::class, MobileDetect::class); // @phpstan-ignore-line
// @codeCoverageIgnoreEnd
}

$detect = new MobileDetect();
$templatePath = ($this->templateFinder)($name);
$detect = new Mobile_Detect(null, $this->userAgent);
$isMobile = $detect->isMobile() && ! $detect->isTablet();
if ($isMobile) {
$mobilePath = str_replace(TwigRenderer::EXT, '.mobile.twig', $templatePath);
Expand Down
3 changes: 2 additions & 1 deletion src/MobileTwigModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
class MobileTwigModule extends AbstractModule
{
/**
* {@inheritdoc}
* {@inheritDoc}
*/
protected function configure()
{
$this->bind(TemplateFinderInterface::class)->to(MobileTemplateFinder::class);
$this->bind(TemplateFinderInterface::class)->annotatedWith('original')->to(TemplateFinder::class);
}
}
2 changes: 1 addition & 1 deletion src/OptionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function get()
{
Expand Down
11 changes: 7 additions & 4 deletions src/TemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

namespace Madapaja\TwigModule;

use function assert;
use function is_int;
use function str_replace;
use function strpos;
use function substr;

class TemplateFinder implements TemplateFinderInterface
{
/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function __invoke(string $resourceFilePath): string
public function __invoke(string $name): string
{
$pos = strpos($resourceFilePath, '/Resource/');
$relativePath = substr($resourceFilePath, $pos + 10);
$pos = strpos($name, '/Resource/');
assert(is_int($pos));
$relativePath = substr($name, $pos + 10);

return str_replace('.php', TwigRenderer::EXT, $relativePath);
}
Expand Down
4 changes: 2 additions & 2 deletions src/TwigErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function handle(Throwable $e, Request $request)
{
Expand Down Expand Up @@ -56,7 +56,7 @@ public function handle(Throwable $e, Request $request)
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function transfer()
{
Expand Down
10 changes: 7 additions & 3 deletions src/TwigErrorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

class TwigErrorPage extends ResourceObject
{
/** @var array */
/** @var array<string, string> */
public $headers = ['content-type' => 'text/html; charset=utf-8'];

/** @var RenderInterface */
protected $renderer;

public function __sleep()
Expand All @@ -23,12 +25,14 @@ public function __sleep()
/**
* @Inject
* @Named("error_page")
* {@inheritDoc}
*/
#[Inject]

#[Named('error_page')]
public function setRenderer(RenderInterface $renderer): void
public function setRenderer(RenderInterface $renderer)
{
$this->renderer = $renderer;

return $this;
}
}
73 changes: 0 additions & 73 deletions src/TwigErrorPageHandler.php

This file was deleted.

Loading

0 comments on commit 36c4809

Please sign in to comment.