Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: Debugger modes #1

Merged
merged 11 commits into from Sep 11, 2021
18 changes: 9 additions & 9 deletions .github/workflows/code_analysis.yaml
Expand Up @@ -17,14 +17,14 @@ jobs:
- name: PHPStan
run: composer phpstan

- name: Easy Coding Standard
run: composer ecs

- name: Nette Tester
run: composer tester
php:
- "7.4"
- "8.0"
- "8.1"


name: ${{ matrix.actions.name }}
name: ${{ matrix.actions.name }} on PHP ${{ matrix.php }}
runs-on: ubuntu-latest


Expand All @@ -37,7 +37,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: ${{ matrix.php }}
coverage: none


Expand All @@ -52,7 +52,7 @@ jobs:
with:
path: |
${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-data-${{ hashFiles('composer.json') }}
key: ${{ runner.os }}-composer-data-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}
restore-keys: |
${{ runner.os }}-composer-data-

Expand All @@ -61,11 +61,11 @@ jobs:
with:
path: |
**/composer.lock
key: ${{ runner.os }}-composer-lock-${{ hashFiles('composer.json') }}
key: ${{ runner.os }}-composer-lock-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}


- name: Install Composer
run: composer install --no-progress


- run: ${{ matrix.actions.run }}
- run: ${{ matrix.actions.run }}
37 changes: 37 additions & 0 deletions .phpstorm.meta.php
@@ -0,0 +1,37 @@
<?php

namespace PHPSTORM_META {

expectedArguments(
\Redbitcz\DebugMode\Detector::__construct(),
0,
\Redbitcz\DebugMode\Detector::MODE_FULL,
\Redbitcz\DebugMode\Detector::MODE_SIMPLE,
\Redbitcz\DebugMode\Detector::MODE_ENABLER,
\Redbitcz\DebugMode\Detector::MODE_COOKIE,
\Redbitcz\DebugMode\Detector::MODE_ENV,
\Redbitcz\DebugMode\Detector::MODE_IP
);

expectedArguments(
\Redbitcz\DebugMode\Detector::detect(),
0,
\Redbitcz\DebugMode\Detector::MODE_FULL,
\Redbitcz\DebugMode\Detector::MODE_SIMPLE,
\Redbitcz\DebugMode\Detector::MODE_ENABLER,
\Redbitcz\DebugMode\Detector::MODE_COOKIE,
\Redbitcz\DebugMode\Detector::MODE_ENV,
\Redbitcz\DebugMode\Detector::MODE_IP
);

expectedArguments(
\Redbitcz\DebugMode\Detector::detectProductionMode(),
0,
\Redbitcz\DebugMode\Detector::MODE_FULL,
\Redbitcz\DebugMode\Detector::MODE_SIMPLE,
\Redbitcz\DebugMode\Detector::MODE_ENABLER,
\Redbitcz\DebugMode\Detector::MODE_COOKIE,
\Redbitcz\DebugMode\Detector::MODE_ENV,
\Redbitcz\DebugMode\Detector::MODE_IP
);
}
19 changes: 12 additions & 7 deletions README.md
Expand Up @@ -9,7 +9,7 @@ at any environment.
Package allows your app to switch to Debug Mode:
- automatically on localhost's environment by IP,
- semi-automatically on any environment where you set `APP_DEBUG` environment variable (useful for Docker dev-stack),
- semi-automatically disable Debug mode on any environment where you set `app-debug-mode` cookie variable (useful for
- semi-automatically **disable** Debug mode on any environment where you set `app-debug-mode` cookie variable (useful for
tests and similar cases),
- manually enable/disable (force turn-on or turn-off) Debug Mode.

Expand All @@ -20,7 +20,10 @@ Package is optimized for invoking in very early lifecycle phase of your App
## Requirements
Package requires:

- PHP version at least 7.3
- PHP version at least 7.4

Enabler requires:

- Temporary directory with writable access

## Installation
Expand All @@ -31,17 +34,15 @@ composer require redbitcz/debug-mode-enabler
## Using
Anywhere in your app you can determine if app is running in Debug mode by simple code:
```php
$debugMode = \Redbitcz\DebugMode\Detector::detect($tempDir); //bool
$debugMode = \Redbitcz\DebugMode\Detector::detect(); //bool
```
where `$tempDir` is required path to temporary directory.

It returns `$debugMode` = `true` when it detects Debug environment or manually switched.

### Using with Nette
In Boostrap use package like in this example:
```php
$tempDir = __DIR__ . '/../temp';
$debugModeDetector = new \Redbitcz\DebugMode\Detector($tempDir);
$debugModeDetector = new \Redbitcz\DebugMode\Detector();

$configurator = new Configurator();
$configurator->setDebugMode($debugModeDetector->isDebugMode());
Expand Down Expand Up @@ -72,6 +73,9 @@ Enabler provide feature to force enable or disable Debug Mode anywhere for user'
This example turn on Debug Mode for user's browser:
```php
$enabler = new \Redbitcz\DebugMode\Enabler($tempDir);

$detector = new \Redbitcz\DebugMode\Detector(\Redbitcz\DebugMode\Detector::MODE_FULL, $enabler);

$enabler->activate(true);
```

Expand All @@ -93,7 +97,8 @@ internally with `Detector` instance in `Bootstrap`.
To re-use already existing instance you can inject it to DI Container:
```php
$tempDir = __DIR__ . '/../temp';
$debugModeDetector = new \Redbitcz\DebugMode\Detector($tempDir);
$enabler = new \Redbitcz\DebugMode\Enabler($tempDir);
$debugModeDetector = new \Redbitcz\DebugMode\Detector(\Redbitcz\DebugMode\Detector::MODE_FULL, $enabler);

$configurator = new Configurator();
$configurator->setDebugMode($debugModeDetector->isDebugMode());
Expand Down
11 changes: 4 additions & 7 deletions composer.json
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=7.3.4",
"php": ">=7.4",
"nette/utils": "^3.0"
},
"autoload": {
Expand All @@ -37,14 +37,11 @@
}
},
"require-dev": {
"phpstan/phpstan": "^0.12.50",
"symplify/easy-coding-standard": "^8.3",
"nette/tester": "^2.3"
"phpstan/phpstan": "^0.12.98",
"nette/tester": "^2.4"
},
"scripts": {
"phpstan": "phpstan analyze src --level 8",
"ecs": "ecs check src tests --set psr12",
"ecs-fix": "ecs check src tests --set psr12 --fix",
"phpstan": "phpstan analyze src -c phpstan.neon --level 8",
"tester": "tester tests"
}
}
6 changes: 6 additions & 0 deletions phpstan.neon
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: '#Parameter \#3 \$options of function setcookie expects .+#'
path: src/Enabler.php
count: 2