Skip to content

Commit a309dc9

Browse files
committed
First commit
0 parents  commit a309dc9

19 files changed

+482
-0
lines changed

.gitattributes

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.gitattributes export-ignore
2+
.github/ export-ignore
3+
.gitignore export-ignore
4+
.idea export-ignore
5+
tests/ export-ignore
6+
examples/ export-ignore
7+
assets/ export-ignore
8+

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: SRWieZ

.github/workflows/pint.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Linting
2+
on:
3+
workflow_dispatch:
4+
push:
5+
branches-ignore:
6+
- 'dependabot/npm_and_yarn/*'
7+
jobs:
8+
pint:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: '8.3'
20+
tools: composer:v2
21+
22+
- name: Copy .env
23+
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
24+
25+
- name: Install Dependencies
26+
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
27+
28+
- name: Launch Pint inspection
29+
run: vendor/bin/pint
30+
31+
- name: Commit changes
32+
uses: stefanzweifel/git-auto-commit-action@v4
33+
with:
34+
commit_message: PHP Linting (Pint)
35+
skip_fetch: true

.github/workflows/test.yml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
test:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
os: [ ubuntu-latest, windows-latest, macos-latest ]
16+
php: [ 8.3, 8.4 ]
17+
stability: [ prefer-lowest, prefer-stable ]
18+
19+
name: Test with php ${{ matrix.php }} on ${{ matrix.os }} - ${{ matrix.stability }}
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Setup PHP
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: ${{ matrix.php }}
29+
tools: composer:v2
30+
# coverage: xdebug
31+
32+
- name: Get composer cache directory (Unix based)
33+
if: runner.os != 'Windows'
34+
id: composer-cache-unix
35+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
36+
37+
- name: Get composer cache directory (Windows)
38+
if: runner.os == 'Windows'
39+
id: composer-cache-windows
40+
run: |
41+
$dir = composer config cache-files-dir
42+
echo "dir=$dir" >> $env:GITHUB_OUTPUT
43+
44+
- name: Cache dependencies
45+
uses: actions/cache@v4
46+
with:
47+
path: ${{ steps.composer-cache-unix.outputs.dir || steps.composer-cache-windows.outputs.dir }}
48+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
49+
restore-keys: ${{ runner.os }}-composer-
50+
51+
- name: Install Dependencies
52+
run: composer update --no-interaction --prefer-dist --${{ matrix.stability }}
53+
54+
- name: Run tests
55+
run: ./vendor/bin/pest

.gitignore

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Ignore vendor directory
2+
/vendor
3+
4+
# Ignore composer.lock file
5+
composer.lock
6+
7+
# Ignore .env file (if used)
8+
.env
9+
10+
# Ignore IDE-specific files and directories
11+
.idea/
12+
.vscode/
13+
*.sublime-*
14+
15+
# Ignore log and cache directories
16+
/logs/
17+
/cache/
18+
.phpunit.result.cache
19+
20+

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Eser DENIZ
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
WIP WIP WIP
2+
3+
# FlushDNS
4+
5+
[//]: # ([![Latest Stable Version](https://poser.pugx.org/knotsphp/flushdns/v)](https://packagist.org/packages/knotsphp/flushdns) )
6+
[//]: # ([![Total Downloads](https://poser.pugx.org/knotsphp/flushdns/downloads)](https://packagist.org/packages/knotsphp/flushdns) )
7+
[//]: # ([![Latest Unstable Version](https://poser.pugx.org/knotsphp/flushdns/v/unstable)](https://packagist.org/packages/knotsphp/flushdns) )
8+
[//]: # ([![License](https://poser.pugx.org/knotsphp/flushdns/license)](https://packagist.org/packages/knotsphp/flushdns) )
9+
[//]: # ([![PHP Version Require](https://poser.pugx.org/knotsphp/flushdns/require/php)](https://packagist.org/packages/knotsphp/flushdns) )
10+
[//]: # ([![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/knotsphp/flushdns/test.yml?label=Tests)](https://github.com/knotsphp/flushdns/actions/workflows/test.yml))
11+
12+
FlushDNS is a PHP library to flush the DNS cache of the current machine.
13+
14+
15+
## 🚀 Installation
16+
17+
```bash
18+
composer require knotsphp/flushdns
19+
```
20+
21+
## 📚 Usage
22+
WIP WIP WIP
23+
24+
```php
25+
use KnotsPHP\FlushDNS\FlushDNS;
26+
27+
// Flush DNS cache
28+
$success = FlushDNS::run();
29+
30+
// Only get the command
31+
$command = FlushDNS::getCommand();
32+
33+
// Check if the command needs elevated privileges
34+
$needsElevation = FlushDNS::needsElevation();
35+
```
36+
37+
## 📚 Use in command line
38+
39+
You can also use this library in the command line by using the `flushdns` command.
40+
41+
It's recommended to install the library globally to use it in the command line.
42+
```bash
43+
composer global require knotsphp/flushdns
44+
```
45+
46+
Then you can use the `flushdns` command to get the public IP address of the current machine.
47+
```bash
48+
# In your project directory
49+
vendor/bin/flushdns
50+
51+
# Globally installed
52+
flushdns
53+
```
54+
55+
## 📖 Documentation
56+
This library is compatible with MacOS, Linux, and Windows.
57+
58+
Some operating systems may require root access to flush the DNS cache.
59+
60+
## 🤝 Contributing
61+
Clone the project and run `composer update` to install the dependencies.
62+
63+
Before pushing your changes, run `composer qa`.
64+
65+
This will run [pint](http://github.com/laravel/pint) (code style), [phpstan](http://github.com/phpstan/phpstan) (static analysis), and [pest](http://github.com/pestphp/pest) (tests).
66+
67+
## 👥 Credits
68+
69+
FlushDNS was created by Eser DENIZ.
70+
71+
## 📝 License
72+
73+
FlushDNS is licensed under the MIT License. See LICENSE for more information.

bin/flushdns

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
include $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php';
5+
6+
require __DIR__.'/../cli/flushdns.php';

cli/flushdns.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use KnotsPHP\FlushDNS\FlushDNS;
5+
6+
error_reporting(E_ALL ^ E_DEPRECATED ^ E_NOTICE);
7+
8+
if (! class_exists('\Composer\InstalledVersions')) {
9+
require __DIR__.'/../vendor/autoload.php';
10+
}
11+
12+
if (class_exists('\NunoMaduro\Collision\Provider')) {
13+
(new \NunoMaduro\Collision\Provider)->register();
14+
}
15+
16+
$ipVersion = null;
17+
18+
// if --help or -h is passed, show help
19+
if (in_array('--help', $argv) || in_array('-h', $argv)) {
20+
echo 'Usage: flushdns'.PHP_EOL;
21+
echo PHP_EOL;
22+
exit(0);
23+
}
24+
25+
$command = FlushDNS::getCommand();
26+
27+
echo '> '.$command.PHP_EOL;
28+
29+
FlushDNS::run();

composer.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "knotsphp/flushdns",
3+
"description": "Flush DNS cache on your system",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"KnotsPHP\\FlushDNS\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Eser DENIZ",
14+
"email": "srwiez@gmail.com"
15+
}
16+
],
17+
"bin": [
18+
"bin/flushdns"
19+
],
20+
"minimum-stability": "stable",
21+
"require": {
22+
"php": "^8.2",
23+
"ext-curl": "*",
24+
"symfony/process": "^7.2"
25+
},
26+
"require-dev": {
27+
"laravel/pint": "^1.18",
28+
"pestphp/pest": "^3.5",
29+
"phpstan/phpstan": "^2.0",
30+
"symfony/var-dumper": "^7.2",
31+
"spatie/ray": "^1.41"
32+
},
33+
"config": {
34+
"allow-plugins": {
35+
"pestphp/pest-plugin": true
36+
}
37+
},
38+
"scripts": {
39+
"test": "./vendor/bin/pest -p",
40+
"pint": "./vendor/bin/pint",
41+
"phpstan": "./vendor/bin/phpstan",
42+
"static": [
43+
"@composer pint",
44+
"@composer phpstan"
45+
],
46+
"qa": [
47+
"@composer pint",
48+
"@composer phpstan",
49+
"@composer test"
50+
]
51+
}
52+
}

examples/basic.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use KnotsPHP\FlushDNS\FlushDNS;
4+
5+
require_once __DIR__.'/../vendor/autoload.php';
6+
7+
// Get the command
8+
$command = FlushDNS::getCommand();
9+
10+
echo $command.PHP_EOL;
11+
12+
echo PHP_EOL;

phpstan.neon

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
includes:
2+
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
3+
4+
parameters:
5+
level: max
6+
paths:
7+
- src
8+
# - tests
9+
ignoreErrors:
10+
# -

phpunit.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap="vendor/autoload.php"
4+
colors="true">
5+
<testsuites>
6+
<testsuite name="Test Suite">
7+
<directory>./tests</directory>
8+
</testsuite>
9+
</testsuites>
10+
<source>
11+
<include>
12+
<directory>./src</directory>
13+
</include>
14+
</source>
15+
</phpunit>

src/FlushDNS.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace KnotsPHP\FlushDNS;
4+
5+
class FlushDNS
6+
{
7+
/**
8+
* Get the command to flush the DNS.
9+
*/
10+
public static function getCommand(): string
11+
{
12+
$os = PHP_OS_FAMILY;
13+
14+
if ($os === 'Windows') {
15+
return 'ipconfig /flushdns';
16+
}
17+
18+
if ($os === 'Darwin') {
19+
return 'sudo killall -HUP mDNSResponder';
20+
}
21+
22+
return 'sudo systemd-resolve --flush-caches';
23+
}
24+
25+
/**
26+
* Check if the command needs elevation.
27+
*/
28+
public static function needsElevation(): bool
29+
{
30+
$os = PHP_OS_FAMILY;
31+
32+
return $os !== 'Windows';
33+
}
34+
35+
/**
36+
* Run the command to flush the DNS.
37+
*/
38+
public static function run(): bool
39+
{
40+
$command = self::getCommand();
41+
42+
exec($command, $output, $result_code);
43+
44+
return $result_code === 0;
45+
}
46+
}

0 commit comments

Comments
 (0)