Open Journal Systems  3.3.0
ComposerLintTask.php
1 <?php
9 require_once 'phing/Task.php';
10 
11 class ComposerLintTask extends Task
12 {
13  protected $dir = null;
14  protected $file = null;
15  protected $passthru = false;
16  protected $composer = null;
17 
23  public function setDir($str)
24  {
25  $this->dir = $str;
26  }
27 
33  public function setFile($str)
34  {
35  $this->file = $str;
36  }
37 
43  public function setPassthru($passthru)
44  {
45  $this->passthru = (bool) $passthru;
46  }
47 
55  public function setComposer($str)
56  {
57  $this->file = $str;
58  }
59 
63  public function init()
64  {
65  // nothing needed here
66  }
67 
71  public function main()
72  {
73  if ($this->composer === null) {
74  $this->findComposer();
75  }
76 
77  $files = array();
78  if (!empty($this->file) && file_exists($this->file)) {
79  $files[] = $this->file;
80  }
81 
82  if (!empty($this->dir)) {
83  $found = $this->findFiles();
84  foreach ($found as $file) {
85  $files[] = $this->dir . DIRECTORY_SEPARATOR . $file;
86  }
87  }
88 
89  foreach ($files as $file) {
90 
91  $cmd = $this->composer . ' validate ' . $file;
92  $cmd = escapeshellcmd($cmd);
93 
94  if ($this->passthru) {
95  $retval = null;
96  passthru($cmd, $retval);
97  if ($retval == 1) {
98  throw new BuildException('invalid composer.json');
99  }
100  } else {
101  $out = array();
102  $retval = null;
103  exec($cmd, $out, $retval);
104  if ($retval == 1) {
105  $err = join("\n", $out);
106  throw new BuildException($err);
107  } else {
108  $this->log($out[0]);
109  }
110  }
111 
112  }
113 
114  }
115 
121  protected function findFiles()
122  {
123  $ds = new DirectoryScanner();
124  $ds->setBasedir($this->dir);
125  $ds->setIncludes(array('**/composer.json'));
126  $ds->scan();
127  return $ds->getIncludedFiles();
128  }
129 
134  protected function findComposer()
135  {
136  $basedir = $this->project->getBasedir();
137  $php = $this->project->getProperty('php.interpreter');
138 
139  if (file_exists($basedir . '/composer.phar')) {
140  $this->composer = "$php $basedir/composer.phar";
141  } else {
142  $out = array();
143  exec('which composer', $out);
144  if (empty($out)) {
145  throw new BuildException(
146  'Could not determine composer location.'
147  );
148  }
149  $this->composer = $out[0];
150  }
151  }
152 }
ComposerLintTask\setFile
setFile($str)
Definition: ComposerLintTask.php:33
ComposerLintTask\init
init()
Definition: ComposerLintTask.php:63
ComposerLintTask\setComposer
setComposer($str)
Definition: ComposerLintTask.php:55
ComposerLintTask\$file
$file
Definition: ComposerLintTask.php:14
ComposerLintTask\findComposer
findComposer()
Definition: ComposerLintTask.php:134
ComposerLintTask\main
main()
Definition: ComposerLintTask.php:71
ComposerLintTask\$dir
$dir
Definition: ComposerLintTask.php:13
ComposerLintTask\setPassthru
setPassthru($passthru)
Definition: ComposerLintTask.php:43
ComposerLintTask\findFiles
findFiles()
Definition: ComposerLintTask.php:121
ComposerLintTask
Definition: ComposerLintTask.php:11
ComposerLintTask\setDir
setDir($str)
Definition: ComposerLintTask.php:23
ComposerLintTask\$passthru
$passthru
Definition: ComposerLintTask.php:15
ComposerLintTask\$composer
$composer
Definition: ComposerLintTask.php:16