1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\Finder\Shell;
13:
14: /**
15: * @author Jean-François Simon <contact@jfsimon.fr>
16: */
17: class Shell
18: {
19: const TYPE_UNIX = 1;
20: const TYPE_DARWIN = 2;
21: const TYPE_CYGWIN = 3;
22: const TYPE_WINDOWS = 4;
23: const TYPE_BSD = 5;
24:
25: /**
26: * @var string|null
27: */
28: private $type;
29:
30: /**
31: * Returns guessed OS type.
32: *
33: * @return int
34: */
35: public function getType()
36: {
37: if (null === $this->type) {
38: $this->type = $this->guessType();
39: }
40:
41: return $this->type;
42: }
43:
44: /**
45: * Tests if a command is available.
46: *
47: * @param string $command
48: *
49: * @return bool
50: */
51: public function testCommand($command)
52: {
53: if (self::TYPE_WINDOWS === $this->type) {
54: // todo: find a way to test if windows command exists
55: return false;
56: }
57:
58: if (!function_exists('exec')) {
59: return false;
60: }
61:
62: // todo: find a better way (command could not be available)
63: exec('command -v '.$command, $output, $code);
64:
65: return 0 === $code && count($output) > 0;
66: }
67:
68: /**
69: * Guesses OS type.
70: *
71: * @return int
72: */
73: private function guessType()
74: {
75: $os = strtolower(PHP_OS);
76:
77: if (false !== strpos($os, 'cygwin')) {
78: return self::TYPE_CYGWIN;
79: }
80:
81: if (false !== strpos($os, 'darwin')) {
82: return self::TYPE_DARWIN;
83: }
84:
85: if (false !== strpos($os, 'bsd')) {
86: return self::TYPE_BSD;
87: }
88:
89: if (0 === strpos($os, 'win')) {
90: return self::TYPE_WINDOWS;
91: }
92:
93: return self::TYPE_UNIX;
94: }
95: }
96: