gist/src/Gist/Service/Gist.php

190 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2015-05-06 14:11:00 +02:00
<?php
namespace Gist\Service;
use Gist\Model\Gist as GistModel;
2015-05-06 14:11:00 +02:00
use GitWrapper\GitWorkingCopy;
2015-05-06 22:24:42 +02:00
use GitWrapper\GitWrapper;
use GitWrapper\GitCommand;
use GeSHi;
2015-11-10 22:33:19 +01:00
use Gist\Model\GistQuery;
use Gist\Model\User;
2015-05-06 14:11:00 +02:00
/**
2016-11-13 00:44:23 +01:00
* Class Gist.
*
2015-05-06 14:11:00 +02:00
* @author Simon Vieille <simon@deblan.fr>
*/
class Gist
2015-05-06 14:11:00 +02:00
{
2016-11-13 00:44:23 +01:00
/**
* @var string
*/
2015-05-06 14:11:00 +02:00
protected $gistPath;
2016-11-13 00:44:23 +01:00
/**
* @var GitWrapper
*/
2015-05-06 22:24:42 +02:00
protected $gitWrapper;
2015-05-06 14:11:00 +02:00
2016-11-13 00:44:23 +01:00
/**
* @var GitWorkingCopy
*/
2015-05-06 22:24:42 +02:00
protected $gitWorkingCopy;
2016-11-13 00:44:23 +01:00
/**
* __construct.
*
* @param mixed $gistPath
* @param GitWrapper $gitWrapper
* @param GitWorkingCopy $gitWorkingCopy
*/
2017-10-15 02:07:41 +02:00
public function __construct($gistPath, GitWrapper $gitWrapper, GitWorkingCopy $gitWorkingCopy)
2015-05-06 14:11:00 +02:00
{
$this->gistPath = $gistPath;
2015-05-06 22:24:42 +02:00
$this->gitWrapper = $gitWrapper;
$this->gitWorkingCopy = $gitWorkingCopy;
}
2016-11-13 00:44:23 +01:00
/**
* Returns a collection of gists.
*
* @return Propel\Runtime\Collection\ObjectCollection
*/
2015-11-10 22:33:19 +01:00
public function getGists()
{
return GistQuery::create()->find();
}
2016-11-13 00:44:23 +01:00
/**
* Returns the history of a Gist.
*
* @param GistModel $gist
*
* @return array
*/
public function getHistory(GistModel $gist)
2015-05-06 22:24:42 +02:00
{
$command = GitCommand::getInstance('log', '--format=medium', $gist->getFile());
$command->setDirectory($this->gistPath);
$command->bypass(false);
$output = $this->gitWrapper->run($command);
preg_match_all('/commit ([^\n]+)\n/isU', $output, $commits, PREG_SET_ORDER);
preg_match_all('/Date:\s+([^\n]+)\n/isU', $output, $dates, PREG_SET_ORDER);
$history = [];
2016-11-13 00:44:23 +01:00
for ($i = count($commits) - 1; $i >= 0; --$i) {
2015-05-06 22:46:29 +02:00
$commit = trim($commits[$i][1]);
2015-05-07 00:35:31 +02:00
$command = GitCommand::getInstance('show', '--no-color', $commit);
2015-05-06 22:46:29 +02:00
$command->setDirectory($this->gistPath);
$command->bypass(false);
$diff = explode("\n", $this->gitWrapper->run($command));
$diff = implode("\n", array_slice($diff, 11));
$data = array(
2015-05-06 22:24:42 +02:00
'commit' => trim($commits[$i][1]),
'date' => new \DateTime(trim($dates[$i][1])),
2017-10-15 02:07:41 +02:00
'diff' => str_replace('\ No newline at end of file', '', $diff),
2015-05-06 22:24:42 +02:00
);
2015-05-06 22:46:29 +02:00
2016-09-24 16:07:35 +02:00
if ($gist->isCipher()) {
$data['content'] = $this->getContent($gist, $commit);
}
2015-05-06 22:46:29 +02:00
$history[] = $data;
2015-05-06 22:24:42 +02:00
}
return $history;
}
2016-11-13 00:44:23 +01:00
/**
* Returns the content of a gist.
*
* @param GistModel $gist
* @param string $commit
*
* @return string
*/
public function getContent(GistModel $gist, $commit)
2015-05-06 22:24:42 +02:00
{
2015-05-07 13:38:24 +02:00
$command = GitCommand::getInstance('cat-file', '-p', $commit.':'.$gist->getFile());
2015-05-06 22:24:42 +02:00
$command->setDirectory($this->gistPath);
$command->bypass(false);
2016-01-02 19:28:57 +01:00
return str_replace("\r\n", "\n", $this->gitWrapper->run($command));
2015-05-06 14:11:00 +02:00
}
2016-11-13 00:44:23 +01:00
/**
* Creates a gist.
*
* @param GistModel $gist
* @param array $data
* @param mixed $user
*
* @return GistModel
*/
public function create(GistModel $gist, array $data, $user = null)
2015-05-06 14:11:00 +02:00
{
$gist->hydrateWith($data);
$gist->generateFilename();
file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']);
2015-05-06 22:24:42 +02:00
$this->gitWorkingCopy
2015-05-06 14:11:00 +02:00
->add($gist->getFile())
->commit('Init');
if (is_object($user) && $user instanceof User) {
$gist->setUser($user);
}
$gist->commit()->save();
2015-05-06 14:11:00 +02:00
return $gist;
}
2015-05-06 22:24:42 +02:00
2016-11-13 00:44:23 +01:00
/**
* Makes a commit.
*
* @param GistModel $gist
* @param array $data
*
* @return GistModel
*/
public function commit(GistModel $gist, array $data)
2015-05-07 14:10:23 +02:00
{
file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']);
$this->gitWorkingCopy
->add($gist->getFile())
->commit('Update');
$gist->commit()->save();
2015-05-07 14:10:23 +02:00
return $gist;
}
/*
* Returns the number of commits.
*
* @param GistModel $gist
*
* @return int
*/
public function getNumberOfCommits(GistModel $gist)
{
$command = GitCommand::getInstance('log', '--oneline', '--', $gist->getFile());
$command->setDirectory($this->gistPath);
$command->bypass(false);
$content = trim($this->gitWrapper->run($command));
$content = str_replace("\r\n", "\n", $content);
return count(explode("\n", $content));
}
2015-05-06 14:11:00 +02:00
}