File tree 10 files changed +269
-0
lines changed
10 files changed +269
-0
lines changed Original file line number Diff line number Diff line change @@ -4,3 +4,4 @@ composer.phar
4
4
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
5
5
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6
6
# composer.lock
7
+ .idea /
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments