-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-cs-fixer.php
executable file
·127 lines (105 loc) · 2.84 KB
/
php-cs-fixer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /usr/bin/php -q
<?php
// -------------------------------------------------------------------
// PHP Script for CotEditor
// -------------------------------------------------------------------
// PURPOSE
// Format PHP code with php-cs-fixer
//
// -------------------------------------------------------------------
// INSTALL
// 1. In the CotEditor menu, select __Scripts__ > __Open Scripts Folder__.
// 2. Put scripts in the folder.
// See https://github.com/coteditor/SampleScripts for more information
//
// -------------------------------------------------------------------
// RELATED
// https://github.com/coteditor/SampleScripts
// https://github.com/coteditor/CotEditor
// https://coteditor.com/
//
// -------------------------------------------------------------------
// PREREQUISITES
// Install php-cs-fixer from
// http://cs.sensiolabs.org/
// https://github.com/FriendsOfPHP/PHP-CS-Fixer
//
// -------------------------------------------------------------------
try {
// Write to console
fwrite(STDERR, "*** START ***\n");
if ($argc < 2 )
{
// ERROR
// Wrong number of arguments
// $argv[0] = Name of the script
// $argv[1] = File in editor
throw new \Exception('Wrong number of arguments', 1);
}
if (empty($argv[1]))
{
// ERROR
// Filename is missing
// $argv[1] = File in editor
throw new \Exception('Filename is missing', 1);
}
// Get path to active file in editor
$file = $argv[1];
if (empty($argv[1])) {
// ERROR
// Filename is missing
// $argv[1] = File in editor
throw new \Exception('Filename is missing', 1);
}
// Check if extension is php or inc
$extension = pathinfo($file, PATHINFO_EXTENSION); // e.g. "php"
$extension = strtolower($extension);
if ($extension === 'php' || $extension === 'inc' || $extension === 'inc.php') {
// SUCCESS
// php extension
// Continue
} else {
// ERROR
// Wrong file extension
// Seems to be no PHP file
throw new \Exception('Extension "'. $extension .'" is wrong', 1);
}
$binary = '/usr/local/bin/php-cs-fixer';
if (file_exists($binary) && is_executable($binary)){
// SUCCESS
// Binary exists
// Binary is executable
} else {
// ERROR
// Wrong number of arguments
// $argv[0] = Name of the script
// $argv[1] = File in editor
throw new \Exception('Binary "'. $binary .'" not found. Get it at http://cs.sensiolabs.org', 1);
}
$cmd = '';
$cmd .= $binary;
$cmd .= ' ';
$cmd .= 'fix';
$cmd .= ' ';
$cmd .= '--using-cache=no';
$cmd .= ' ';
$cmd .= '"'. $file .'"';
if (0) {
// Set logfile
$log = $file . '.log';
// Redirect output, and write to file
$cmd .= ' ';
$cmd .= '2>&1';
$cmd .= ' > ';
$cmd .= $log;
}
fwrite(STDERR, "*** EXEC ***\n");
fwrite(STDERR, $cmd ."\n");
shell_exec( $cmd );
} catch (Exception $e) {
fwrite(STDERR, '*** ERROR ***');
fwrite(STDERR, "\n");
fwrite(STDERR, $e->getMessage());
fwrite(STDERR, "\n");
}
fwrite(STDERR, "*** END ***\n");