Skip to content
Snippets Groups Projects
Commit ce81b43c authored by Benjamin Bellamy's avatar Benjamin Bellamy :speech_balloon:
Browse files

Convert class back to script

parent e61c1246
Branches
No related tags found
No related merge requests found
# user-agents-php
PHP implementation for [opawg / user-agents](https://github.com/opawg/user-agents)
This is a dummy PHP implementation for [opawg / user-agents](https://github.com/opawg/user-agents)
## Installation
### Via composer
- Add ``podlibre/user-agents-php` to your `composer.json`.
- Add ``post-install-cmd` / `post-update-cmd` scripts to your `composer.json` so that the class is generated.
```
{
"require": {
"podlibre/user-agents-php": "*"
},
"scripts": {
"post-install-cmd": "@php vendor/podlibre/user-agents-php/src/UserAgentsGenerate.php > vendor/podlibre/user-agents-php/src/UserAgents.php",
"post-update-cmd": "@php vendor/podlibre/user-agents-php/src/UserAgentsGenerate.php > vendor/podlibre/user-agents-php/src/UserAgents.php"
}
}
```
### Manually
- Clone git repository where you need it:
```
$ git clone https://github.com/podlibre/user-agents-php.git
```
- Generate the class:
```
$ php src/UserAgentsGenerate.php > src/UserAgents.php
```
Or with composer:
```
$ composer run-script post-install-cmd
```
## Usage
When you need it, just call `\Podlibre\UserAgentsPhp\UserAgents::find()`:
```
$player = \Podlibre\UserAgentsPhp\UserAgents::find($_SERVER['HTTP_USER_AGENT']);
if($player){
print player['app']."\n";
print player['device']."\n";
print player['os']."\n";
print player['bot']."\n";
} else {
print "This user-agent was not found\n";
}
```
\ No newline at end of file
# user-agents-php
This is a dummy PHP implementation for [opawg / user-agents](https://github.com/opawg/user-agents)
## Installation
### Via composer
- Add ``podlibre/user-agents-php` to your `composer.json`.
- Add ``post-install-cmd` / `post-update-cmd` scripts to your `composer.json` so that the class is generated.
```
{
"require": {
"podlibre/user-agents-php": "*"
},
"scripts": {
"post-install-cmd": "@php vendor/podlibre/user-agents-php/src/UserAgentsGenerate.php > vendor/podlibre/user-agents-php/src/UserAgents.php",
"post-update-cmd": "@php vendor/podlibre/user-agents-php/src/UserAgentsGenerate.php > vendor/podlibre/user-agents-php/src/UserAgents.php"
}
}
```
### Manually
- Clone git repository where you need it:
```
$ git clone https://github.com/podlibre/user-agents-php.git
```
- Generate the class:
```
$ php src/UserAgentsGenerate.php > src/UserAgents.php
```
Or with composer:
```
$ composer run-script post-install-cmd
```
## Usage
When you need it, just call `\Podlibre\UserAgentsPhp\UserAgents::find()`:
```
$player = \Podlibre\UserAgentsPhp\UserAgents::find($_SERVER['HTTP_USER_AGENT']);
if($player){
print player['app']."\n";
print player['device']."\n";
print player['os']."\n";
print player['bot']."\n";
} else {
print "This user-agent was not found\n";
}
```
\ No newline at end of file
......@@ -16,7 +16,7 @@
}
},
"scripts": {
"post-install-cmd": "Podlibre\\UserAgentsPhp\\UserAgentsGenerate::generate",
"post-update-cmd": "Podlibre\\UserAgentsPhp\\UserAgentsGenerate.php::generate"
"post-install-cmd": "@php src/UserAgentsGenerate.php > src/UserAgents.php",
"post-update-cmd": "@php src/UserAgentsGenerate.php > src/UserAgents.php"
}
}
<?php
namespace Podlibre\UserAgentsPhp;
#!/usr/bin/php
class UserAgentsGenerate {
<?php
/*
* Load json and convert it to PHP object:
*/
public static function generate($userAgent) {
$rowstr = var_export(
json_decode(
file_get_contents(
......@@ -17,14 +15,15 @@ class UserAgentsGenerate {
);
// autogenerate database
$code = <<<EOT
print <<<EOT
<?php
namespace Podlibre\UserAgentsPhp;
/* Autogenerated. Do not edit */
class UserAgents {
public static function find(\$userAgent) {
public static function find(\$userAgent)
{
\$playerFound = null;
//Search for current HTTP_USER_AGENT:
......@@ -48,7 +47,10 @@ class UserAgents {
return \$playerFound;
}
EOT;
file_put_contents('UserAgents.php', $code." static public \$db = ".$rowstr.";\n}\n");
}
}
\ No newline at end of file
print " static public \$db = ";
print $rowstr;
print ";\n";
print "}\n";
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment