Skip to content

Commit 7c318a0

Browse files
author
Zsolt Gál
committed
Initial commit
1 parent 6ba71ad commit 7c318a0

10 files changed

+269
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ composer.phar
44
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
55
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
66
# composer.lock
7+
.idea/

composer.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "technodelight/php-cli-cross-platform-open",
3+
"description": "Opens a file or URL in the user's preferred application in CLI (cross platform)",
4+
"type": "library",
5+
"require": {
6+
"technodelight/shell-exec": "^1.0"
7+
},
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "Zsolt Gál",
12+
"email": "zenc@zenc.hu"
13+
}
14+
],
15+
"autoload": {
16+
"psr-4": {"Technodelight\\CliOpen\\": "src"}
17+
},
18+
"scripts": {
19+
"test": [
20+
"find src -type f -name \"*.php\" | xargs -n1 php -l",
21+
"composer diagnose"
22+
]
23+
}
24+
}

composer.lock

+55
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CliOpen.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Technodelight\CliOpen;
4+
5+
class CliOpen
6+
{
7+
/**
8+
* @var Driver
9+
*/
10+
private $driver;
11+
12+
public function __construct(Driver $driver)
13+
{
14+
$this->driver = $driver;
15+
}
16+
17+
public function open($uri)
18+
{
19+
$this->driver->open($uri);
20+
}
21+
}

src/Driver.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Technodelight\CliOpen;
4+
5+
interface Driver
6+
{
7+
/**
8+
* @param string $uri
9+
* @throws Exception
10+
* @return void
11+
*/
12+
public function open($uri);
13+
}

src/Driver/Generic.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Technodelight\CliOpen\Driver;
4+
5+
use Technodelight\CliOpen\Driver;
6+
use Technodelight\CliOpen\Exception;
7+
use Technodelight\ShellExec\Command;
8+
use Technodelight\ShellExec\Shell;
9+
use Technodelight\ShellExec\ShellCommandException;
10+
11+
class Generic implements Driver
12+
{
13+
/**
14+
* @var Shell
15+
*/
16+
private $shell;
17+
18+
public function __construct(Shell $shell)
19+
{
20+
$this->shell = $shell;
21+
}
22+
23+
/**
24+
* @param string $uri
25+
* @throws Exception
26+
* @return void
27+
*/
28+
public function open($uri)
29+
{
30+
try {
31+
$this->shell->exec(
32+
Command::create('open')->withArgument($uri)
33+
);
34+
} catch (ShellCommandException $exception) {
35+
throw Exception::fromUri($uri, $exception);
36+
}
37+
}
38+
}

src/Driver/Opn.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Technodelight\CliOpen\Driver;
4+
5+
use Technodelight\CliOpen\Driver;
6+
use Technodelight\CliOpen\Exception;
7+
use Technodelight\ShellExec\Command;
8+
use Technodelight\ShellExec\Shell;
9+
use Technodelight\ShellExec\ShellCommandException;
10+
11+
class Opn implements Driver
12+
{
13+
/**
14+
* @var Shell
15+
*/
16+
private $shell;
17+
18+
public function __construct(Shell $shell)
19+
{
20+
$this->shell = $shell;
21+
}
22+
23+
/**
24+
* @param string $uri
25+
* @throws Exception
26+
* @return void
27+
*/
28+
public function open($uri)
29+
{
30+
try {
31+
$this->shell->exec(Command::create('opn')->withArgument($uri));
32+
} catch (ShellCommandException $exception) {
33+
Exception::fromUri($uri);
34+
}
35+
}
36+
}

src/Driver/XdgOpen.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Technodelight\CliOpen\Driver;
4+
5+
use Technodelight\CliOpen\Driver;
6+
use Technodelight\CliOpen\Exception;
7+
use Technodelight\ShellExec\Command;
8+
use Technodelight\ShellExec\Shell;
9+
use Technodelight\ShellExec\ShellCommandException;
10+
11+
class XdgOpen implements Driver
12+
{
13+
/**
14+
* @var Shell
15+
*/
16+
private $shell;
17+
18+
public function __construct(Shell $shell)
19+
{
20+
$this->shell = $shell;
21+
}
22+
23+
/**
24+
* @param string $uri
25+
* @throws Exception
26+
* @return void
27+
*/
28+
public function open($uri)
29+
{
30+
try {
31+
$this->shell->exec(Command::create('xdg-open')->withArgument($uri));
32+
} catch (ShellCommandException $exception) {
33+
Exception::fromUri($uri);
34+
}
35+
}
36+
}

src/Exception.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Technodelight\CliOpen;
4+
5+
use RuntimeException;
6+
use Throwable;
7+
8+
class Exception extends RuntimeException
9+
{
10+
const MESSAGE = 'Cannot open %s';
11+
12+
public static function fromUri($uri, Throwable $previous = null)
13+
{
14+
return new self(
15+
sprintf(self::MESSAGE, $uri),
16+
0,
17+
$previous
18+
);
19+
}
20+
}

src/OsAdaptingFactory.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Technodelight\CliOpen;
4+
5+
use Technodelight\CliOpen\Driver\Generic;
6+
use Technodelight\CliOpen\Driver\Opn;
7+
use Technodelight\CliOpen\Driver\XdgOpen;
8+
use Technodelight\ShellExec\Passthru;
9+
10+
class OsAdaptingFactory
11+
{
12+
public static function create()
13+
{
14+
return new CliOpen(self::driver());
15+
}
16+
17+
private static function driver()
18+
{
19+
switch(php_uname('s')) {
20+
case 'Darwin': return new Generic(new Passthru());
21+
case 'Linux': return new XdgOpen(new Passthru());
22+
default: return new Opn(new Passthru());
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)