improve/inc/lib.improve.action.phpcsfix...

100 lines
3.0 KiB
PHP
Raw Normal View History

<?php
/**
* @brief improve, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
class ImproveActionPhpcsfixer extends ImproveAction
{
protected static $errors = [
0 => 'OK.',
1 => 'General error (or PHP minimal requirement not matched).',
4 => 'Some files have invalid syntax (only in dry-run mode).',
8 => 'Some files need fixing (only in dry-run mode).',
16 => 'Configuration error of the application.',
32 => 'Configuration error of a Fixer.',
64 => 'Exception raised within the application'
];
protected function init(): bool
{
$this->setProperties([
'id' => 'phpcsfixer',
'name' => __('PHP CS Fixer'),
'desc' => __('Fix PSR coding style using Php CS Fixer'),
'priority' => 920,
'config' => true,
'types' => ['plugin', 'theme']
]);
return true;
}
public function isConfigured(): bool
{
2021-11-02 00:17:21 +00:00
return true;
}
public function configure($url): ?string
{
if (!empty($_POST['save'])) {
$this->setSettings([
2021-11-02 00:17:21 +00:00
'phpexe_path' => !empty($_POST['phpexe_path']) ? $_POST['phpexe_path'] : ''
]);
$this->redirect($url);
}
return
'<p><label class="classic" for="phpexe_path">' .
__('Root directory of PHP executable:') . '<br />' .
form::field('phpexe_path', 160, 255, $this->getSetting('phpexe_path')) . '</label>' .
'</p>' .
'<p class="form-note">' .
__('If this server is under unix, leave it empty.') . ' ' .
__('If this server is under Windows, put here directory to php executable (without executable file name).') .
2021-11-02 00:17:21 +00:00
' C:\path_to\php</p>';
}
public function closeModule(): ?bool
{
$phpexe_path = path::real($this->getSetting('phpexe_path'));
if (!empty($phpexe_path)) {
$phpexe_path .= '/';
}
$command = sprintf(
2021-11-02 00:17:21 +00:00
'%sphp %s/libs/php-cs-fixer.phar fix %s --config=%s/libs/dc.phpcsfixer.rules.php --using-cache=no',
$phpexe_path,
2021-11-02 00:17:21 +00:00
dirname(__FILE__),
$this->module['sroot'],
dirname(__FILE__)
);
try {
exec($command, $output, $error);
if (empty($output)) {
if (isset(self::$errors[$error])) {
$this->setError(self::$errors[$error]);
return false;
}
throw new Exception('oops');
}
$this->setSuccess(sprintf('<pre>%s</pre>', implode('<br />', $output)));
return true;
} catch (Exception $e) {
$this->setError(__('Failed to run php-cs-fixer'));
return false;
}
}
}