1
+ <?php
2
+
3
+ namespace CV \Installer \Console ;
4
+
5
+ use Symfony \Component \Console \Command \Command ;
6
+ use Symfony \Component \Console \Exception \RuntimeException ;
7
+ use Symfony \Component \Console \Input \InputArgument ;
8
+ use Symfony \Component \Console \Input \InputInterface ;
9
+ use Symfony \Component \Console \Input \InputOption ;
10
+ use Symfony \Component \Console \Output \OutputInterface ;
11
+ use Symfony \Component \Process \Exception \ProcessFailedException ;
12
+ use Symfony \Component \Process \Process ;
13
+
14
+ class InstallCommand extends Command
15
+ {
16
+
17
+ const EXTENSION_NAME = 'opencv ' ;
18
+
19
+ /**
20
+ * Configure the command options.
21
+ *
22
+ * @return void
23
+ */
24
+ protected function configure ()
25
+ {
26
+ $ this
27
+ ->setName ('install ' )
28
+ ->setDescription ('Automatic installation of the php-opencv extension, including automatic installation of opencv (if no opencv is installed) ' )
29
+ ->addArgument ('opencv_build_path ' , InputArgument::OPTIONAL , 'Automatically install the opencv directory ' , '/opt/opencv ' )
30
+ // ->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
31
+ // ->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces install even if the directory already exists')
32
+ ;
33
+ }
34
+
35
+
36
+ protected function buildEnvDetection ()
37
+ {
38
+ $ process = new Process (['./opencv-install-environment-detection.sh ' ]);//给予当前用户
39
+ try {
40
+ $ process ->mustRun ();
41
+ } catch (\Exception $ e ) {
42
+ throw new RuntimeException ($ process ->getErrorOutput ());
43
+ }
44
+ }
45
+
46
+ /**
47
+ * @param string $directory
48
+ */
49
+ protected function cloneOpenCV (string $ directory )
50
+ {
51
+ $ opencvUrl = 'https://github.com/opencv/opencv.git ' ;
52
+ $ command = "sudo git clone {$ opencvUrl } --depth 1 " ;
53
+ $ process = new Process ($ command , $ directory , null , null , null );//给予当前用户
54
+ $ process ->setTty (Process::isTtySupported ());//检查TTY支持
55
+ try {
56
+ $ process ->mustRun ();
57
+ } catch (\Exception $ e ) {
58
+ throw new RuntimeException ($ process ->getErrorOutput ());
59
+ }
60
+ }
61
+
62
+
63
+ protected function cloneOpenCVContrib (string $ directory )
64
+ {
65
+ $ opencvContribUrl = 'https://github.com/opencv/opencv_contrib.git ' ;
66
+ $ command = "sudo git clone {$ opencvContribUrl } --depth 1 " ;
67
+ $ process = new Process ($ command , $ directory , null , null , null );//给予当前用户
68
+ $ process ->setTty (Process::isTtySupported ());//检查TTY支持
69
+ try {
70
+ $ process ->mustRun ();
71
+ } catch (\Exception $ e ) {
72
+ throw new RuntimeException ($ process ->getErrorOutput ());
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Execute the command.
78
+ *
79
+ * @param \Symfony\Component\Console\Input\InputInterface $input
80
+ * @param \Symfony\Component\Console\Output\OutputInterface $output
81
+ *
82
+ * @return void
83
+ */
84
+ protected function execute (InputInterface $ input , OutputInterface $ output )
85
+ {
86
+
87
+
88
+ //判断是否已经安装opencv扩展
89
+ if (extension_loaded (self ::EXTENSION_NAME )) {
90
+ $ process = new Process (['php ' , '--ri ' , self ::EXTENSION_NAME ]);
91
+ $ process ->mustRun ();
92
+ $ output ->writeln ($ process ->getOutput ());
93
+ throw new RuntimeException ('The OpenCV PHP extension is installed. ' );
94
+ }
95
+ $ this ->buildEnvDetection ();
96
+ //创建目录
97
+ $ directory = $ input ->getArgument ('opencv_build_path ' );
98
+ $ output ->writeln ("Compile the directory of opencv with {$ directory }. " );
99
+ if (!file_exists ($ directory )) {
100
+ $ output ->writeln ("Create {$ directory } of the directory " );
101
+ $ process = new Process (['sudo ' , 'mkdir ' , $ directory ]);//给予当前用户
102
+ try {
103
+ $ process ->mustRun ();
104
+ } catch (\Exception $ e ) {
105
+ throw new RuntimeException ($ process ->getErrorOutput ());
106
+ }
107
+
108
+ }
109
+ $ this ->cloneOpenCV ($ directory );
110
+ $ this ->cloneOpenCVContrib ($ directory );
111
+ //编译安装
112
+ $ output ->writeln ('<comment>Application ready! Build something amazing.</comment> ' );
113
+ }
114
+ }
0 commit comments