Serve::serve()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 14
nc 3
nop 2
1
<?php
2
3
namespace Acacha\Llum\Traits;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class Serve.
11
 *
12
 * @property OutputInterface $output
13
 */
14
trait Serve
15
{
16
    /**
17
     * Serve command.
18
     *
19
     * @param InputInterface $input
20
     * @param OutputInterface $output
21
     */
22
    protected function serve(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24
        $port = $this->port($input);
25
        $continue = true;
26
        do {
27
            if ($this->check_port($port)) {
28
                $this->output->writeln('<info>Running php artisan serve --port='.$port.'</info>');
29
                exec('php artisan serve --port='.$port.' > /dev/null 2>&1 &');
30
                sleep(1);
31
                if (file_exists('/usr/bin/sensible-browser')) {
32
                    $this->output->writeln('<info>Opening http://localhost:'.$port.' with default browser</info>');
33
                    passthru('/usr/bin/sensible-browser http://localhost:'.$port);
34
                }
35
                $continue = false;
36
            }
37
            ++$port;
38
        } while ($continue);
39
    }
40
41
    /**
42
     * Check if port is in use.
43
     *
44
     * @param int    $port
45
     * @param string $host
46
     * @param int    $timeout
47
     *
48
     * @return bool
49
     */
50
    protected function check_port($port = 8000, $host = '127.0.0.1', $timeout = 3)
51
    {
52
        $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
53
        if (!$fp) {
54
            return true;
55
        } else {
56
            fclose($fp);
57
58
            return false;
59
        }
60
    }
61
62
    /**
63
     * Configure command.
64
     */
65
    public function configure()
66
    {
67
        parent::configure();
68
69
        $this
70
            // configure an argument
71
            ->addArgument('port', InputArgument::OPTIONAL, 'Port number')
72
        ;
73
    }
74
75
    /**
76
     * Obtain port.
77
     *
78
     * @param InputInterface $input
79
     * @return integer
80
     */
81
    protected function port(InputInterface $input) {
82
        $name = $input->getArgument('port');
83
        return isset($name) ? (int) $name : 8080;
84
    }
85
86
    /**
87
     * Method provided from \Symfony\Component\Console\Command\Command.
88
     *
89
     * @param string $name        The argument name
90
     * @param int    $mode        The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
91
     * @param string $description A description text
92
     * @param mixed  $default     The default value (for InputArgument::OPTIONAL mode only)
93
     *
94
     * @return Command The current instance
95
     */
96
    abstract public function addArgument($name, $mode = null, $description = '', $default = null);
97
}
98