Introduced SystemService

This commit is contained in:
2019-06-04 21:06:27 +02:00
parent fa253e60d8
commit 1159d0b62f
3 changed files with 74 additions and 31 deletions

View File

@@ -6,6 +6,9 @@ services:
config: config:
class: '\Magdev\Dossier\Service\ConfigService' class: '\Magdev\Dossier\Service\ConfigService'
system:
class: '\Magdev\Dossier\Service\SystemService'
monolog: monolog:
class: '\Magdev\Dossier\Service\MonologService' class: '\Magdev\Dossier\Service\MonologService'
@@ -21,7 +24,7 @@ services:
git: git:
class: '\Magdev\Dossier\Service\GitService' class: '\Magdev\Dossier\Service\GitService'
arguments: ['@config', '@monolog'] arguments: ['@config', '@monolog', '@system']
translator: translator:
class: '\Magdev\Dossier\Service\TranslatorService' class: '\Magdev\Dossier\Service\TranslatorService'

View File

@@ -30,14 +30,8 @@
namespace Magdev\Dossier\Service; namespace Magdev\Dossier\Service;
// git branch | grep \* | cut -d ' ' -f2
class GitService class GitService
{ {
const MODE_OUTPUT = 1;
const MODE_RETURN = 2;
const MODE_LASTLINE = 3;
/** /**
* Configuration service * Configuration service
* @var \Magdev\Dossier\Service\ConfigService * @var \Magdev\Dossier\Service\ConfigService
@@ -50,17 +44,25 @@ class GitService
*/ */
protected $logger = null; protected $logger = null;
/**
* System service
* @var \Magdev\Dossier\Service\SystemService
*/
protected $system = null;
/** /**
* Constructor * Constructor
* *
* @param \Magdev\Dossier\Service\ConfigService $config * @param \Magdev\Dossier\Service\ConfigService $config
* @param \Magdev\Dossier\Service\MonologService $logger * @param \Magdev\Dossier\Service\MonologService $logger
* @param \Magdev\Dossier\Service\SystemService $system
*/ */
public function __construct(ConfigService $config, MonologService $logger) public function __construct(ConfigService $config, MonologService $logger, SystemService $system)
{ {
$this->config = $config; $this->config = $config;
$this->logger = $logger; $this->logger = $logger;
$this->system = $system;
} }
@@ -72,7 +74,7 @@ class GitService
public function init(): bool public function init(): bool
{ {
if (!$this->isGitRepository()) { if (!$this->isGitRepository()) {
$this->exec('git init'); $this->system->exec('git init');
} }
return $this->isGitRepository(); return $this->isGitRepository();
} }
@@ -88,7 +90,7 @@ class GitService
if (!$this->isGitRepository()) { if (!$this->isGitRepository()) {
return $this->config->get('output.docname'); return $this->config->get('output.docname');
} }
return $this->exec('git branch | grep \\\* | cut -d \' \' -f2', self::MODE_LASTLINE); return $this->system->exec('git branch | grep \\\* | cut -d \' \' -f2', self::MODE_LASTLINE);
} }
@@ -101,25 +103,4 @@ class GitService
{ {
return is_dir(PROJECT_ROOT.'/.git') && file_exists(PROJECT_ROOT.'/.git/HEAD'); return is_dir(PROJECT_ROOT.'/.git') && file_exists(PROJECT_ROOT.'/.git/HEAD');
} }
/**
* Execute a system command
*
* @param string $cmd
* @param int $mode
* @param array $output
* @param int $return
* @return string|int|array
*/
protected function exec(string $cmd, int $mode = self::MODE_OUTPUT, &$output = array(), int &$return = 0)
{
$lastline = exec($cmd, $output, $return);
switch ($mode) {
case self::MODE_LASTLINE: return $lastline;
case self::MODE_RETURN: return $return;
default:
case self::MODE_OUTPUT: return $output;
}
}
} }

View File

@@ -0,0 +1,59 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2019 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2019 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Service;
class SystemService
{
const MODE_OUTPUT = 1;
const MODE_RETURN = 2;
const MODE_LASTLINE = 3;
/**
* Execute a system command
*
* @param string $cmd
* @param int $mode
* @param array $output
* @param int $return
* @return string|int|array
*/
protected function exec(string $cmd, int $mode = self::MODE_OUTPUT, &$output = array(), int &$return = 0)
{
$lastline = exec($cmd, $output, $return);
switch ($mode) {
case self::MODE_LASTLINE: return $lastline;
case self::MODE_RETURN: return $return;
default:
case self::MODE_OUTPUT: return $output;
}
}
}