aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2019-11-02 21:49:21 +0100
committerWilly Micieli <micieli@laposte.net>2019-11-02 21:49:21 +0100
commit01d4af0d79aae7b18b27f7d4edc53c786b062724 (patch)
treeba59c4b067e71e02f096246ca6e3149c17c7145e
parente595c1b42c673c52aaa7d6c2ad2942f5396a05c6 (diff)
downloadimperium-01d4af0d79aae7b18b27f7d4edc53c786b062724.zip
imperium-01d4af0d79aae7b18b27f7d4edc53c786b062724.tar.gz
rewrite git class
-rw-r--r--imperium/App.php18
-rw-r--r--imperium/Cookies/Cookies.php55
-rw-r--r--imperium/Management.php13
-rw-r--r--imperium/Testing/Unit.php20
-rw-r--r--imperium/Versioning/Git.php178
-rw-r--r--tests/FormTest.php2
-rw-r--r--tests/Versioning/GitTest.php78
7 files changed, 360 insertions, 4 deletions
diff --git a/imperium/App.php b/imperium/App.php
index 1a74dde..7ae1675 100644
--- a/imperium/App.php
+++ b/imperium/App.php
@@ -21,6 +21,7 @@ namespace Imperium {
use Imperium\Session\Session;
use Imperium\Shopping\Shop;
use Imperium\Validator\Validator;
+ use Imperium\Versioning\Git;
use Imperium\View\View;
use Imperium\Writing\Write;
use Imperium\File\File;
@@ -736,5 +737,22 @@ namespace Imperium {
{
return $this->request()->server->get($key,$default);
}
+
+ /**
+ *
+ * Management of git
+ *
+ * @param string $repository
+ * @param string $owner
+ *
+ * @return Git
+ *
+ * @throws Kedavra
+ *
+ */
+ public function git(string $repository, string $owner): Git
+ {
+ return new Git($repository,$owner);
+ }
}
}
diff --git a/imperium/Cookies/Cookies.php b/imperium/Cookies/Cookies.php
index be117e6..14e67e1 100644
--- a/imperium/Cookies/Cookies.php
+++ b/imperium/Cookies/Cookies.php
@@ -19,14 +19,52 @@ namespace Imperium\Cookies {
* @param bool $httpOnly
*
* @return bool
+ *
*/
public function create(string $name, string $value = null, int $expire = 0, ?string $path = '/', string $domain = null, ?bool $secure = false, bool $httpOnly = true): bool
{
+
return setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
}
/**
*
+ * Get all cookies
+ *
+ * @return array
+ *
+ */
+ public function all(): array
+ {
+ return request()->cookies->all();
+ }
+
+ /**
+ *
+ * List cookies keys
+ *
+ * @return array
+ *
+ */
+ public function keys(): array
+ {
+ return request()->cookies->keys();
+ }
+
+ /**
+ *
+ * Return the cookies size
+ *
+ * @return int
+ *
+ */
+ public function size(): int
+ {
+ return request()->cookies->count();
+ }
+
+ /**
+ *
* Check if key exits
*
* @param $key
@@ -36,11 +74,26 @@ namespace Imperium\Cookies {
*/
public function has(string $key): bool
{
- return array_key_exists($key,$_COOKIE);
+ return request()->cookies->has($key);
+
}
/**
*
+ * @param $key
+ * @param $value
+ *
+ * @param int $expire
+ * @return Cookies
+ */
+ public function add($key,$value,int $expire = 0): Cookies
+ {
+ $this->create($key,$value,$expire);
+
+ return $this;
+ }
+ /**
+ *
* Check if a key is define
*
* @param $key
diff --git a/imperium/Management.php b/imperium/Management.php
index 7c9d693..db4f13c 100644
--- a/imperium/Management.php
+++ b/imperium/Management.php
@@ -21,6 +21,7 @@
use Imperium\Tables\Table;
use Imperium\Security\Auth\Oauth;
use Imperium\Validator\Validator;
+ use Imperium\Versioning\Git;
use Imperium\Writing\Write;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -492,5 +493,17 @@
*/
public function validator(array $data): Validator;
+ /**
+ *
+ * Management of git
+ *
+ * @param string $repository
+ * @param string $owner
+ *
+ * @return Git
+ *
+ */
+ public function git(string $repository,string $owner): Git;
+
}
}
diff --git a/imperium/Testing/Unit.php b/imperium/Testing/Unit.php
index a6de2b3..487b62f 100644
--- a/imperium/Testing/Unit.php
+++ b/imperium/Testing/Unit.php
@@ -14,6 +14,7 @@
use Imperium\File\File;
use Imperium\Routing\RouteResult;
use Imperium\Validator\Validator;
+ use Imperium\Versioning\Git;
use Imperium\Writing\Write;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -156,6 +157,23 @@
{
return new Crypt();
}
-
+
+ /**
+ *
+ * Get an instance of git class
+ *
+ * @param string $repository
+ * @param string $owner
+ *
+ * @return Git
+ *
+ * @throws DependencyException
+ * @throws NotFoundException
+ *
+ */
+ public function git(string $repository,string $owner): Git
+ {
+ return app()->git($repository,$owner);
+ }
}
} \ No newline at end of file
diff --git a/imperium/Versioning/Git.php b/imperium/Versioning/Git.php
index 010b5fb..8c62b77 100644
--- a/imperium/Versioning/Git.php
+++ b/imperium/Versioning/Git.php
@@ -2,6 +2,10 @@
namespace Imperium\Versioning;
+ use Imperium\Collection\Collect;
+ use Imperium\Directory\Dir;
+ use Imperium\Exception\Kedavra;
+ use Imperium\Html\Pagination\Pagination;
use Imperium\Redis\Redis;
class Git
@@ -32,6 +36,10 @@
*
*/
private $owner;
+ /**
+ * @var array
+ */
+ private $data;
/**
* Git constructor.
@@ -39,11 +47,179 @@
* @param string $repository
* @param string $owner
*
+ * @throws Kedavra
+ *
*/
public function __construct(string $repository,string $owner)
{
+ is_false(is_dir($repository),true,"Repository is not a directory");
+
+ is_false(chdir($repository),true,"Checkout in repository fail");
+
$this->redis = new Redis();
- $this->repository = $repository;
+
+ $this->repository = equal($repository,'.') ? collect(explode('/',realpath($repository)))->last(): $repository;
+
$this->owner = $owner;
+
+ }
+
+ /**
+ *
+ * Get the repository owner
+ *
+ * @return string
+ *
+ */
+ public function owner(): string
+ {
+ return $this->owner;
+ }
+
+ /**cd
+ *
+ * Get the repository name
+ *
+ * @return string
+ *
+ */
+ public function name():string
+ {
+ return $this->repository;
+ }
+
+ /**
+ *
+ *
+ * Display the status
+ *
+ * @return string
+ *
+ */
+ public function status(): string
+ {
+ return nl2br(html_entity_decode($this->execute('git status | aha')->join("\n")));
}
+
+ /**
+ *
+ * Show diff between two version or from head
+ *
+ * @param string $first_version
+ * @param string $second_version
+ *
+ * @return string
+ *
+ */
+ public function diff(string $first_version ='',string $second_version=''): string
+ {
+ return def($first_version,$second_version) ? html_entity_decode($this->execute("git diff $first_version $second_version -p --stat --color=always | aha ")->join("\n")) : nl2br(html_entity_decode($this->execute('git diff -p --stat --color=always | aha')->join("\n")));
+ }
+
+ /**
+ * @param string $branch
+ * @return int
+ */
+ public function commits_size(string $branch): int
+ {
+ return $this->execute("git rev-list --count $branch")->get(0);
+ }
+
+ /**
+ *
+ * Display all branches
+ *
+ * @return array
+ *
+ */
+ public function branches(): array
+ {
+ $x = collect();
+
+ foreach ($this->execute('git branch') as $branch)
+ {
+ $x->push(str_replace(' ','',str_replace('* ','',$branch)));
+ }
+ return $x->all();
+ }
+
+ /**
+ *
+ * List
+ * @return array
+ *
+ */
+ /**
+ *
+ * Display all releases
+ *
+ * @return array
+ *
+ */
+ public function releases(): array
+ {
+ return $this->execute('git tag -l --sort=v:refname')->reverse()->all();
+ }
+
+ /**
+ *
+ * Display all news between the last release
+ *
+ * @return string
+ *
+ */
+ public function news()
+ {
+ $x = collect($this->releases());
+ return $this->diff($x->get(0),$x->get(1));
+ }
+ public function log(int $current_page)
+ {
+
+ $format = '<div class="card"><div class="card-header" id="commit-%h"><h2 class="mb-0"><button class="btn btn-link" type="button" data-toggle="collapse" data-target="#commit-%H" aria-controls="commit-%H">%s</button></h2></div><div id="commit-%H" class="collapse" aria-labelledby="commit-%H" data-parent="#logs"><div class="card-body"><p>%s</p><div class="text-center"><a href="mailto:%ae" class="btn btn-outline-primary">%an</a></div></div><div class="card-footer text-muted">%cr</div></div></div>';
+
+ $pagination = (new Pagination($current_page,100,$this->commits_size($this->current_branch())))->paginate();
+ $html = '<div class="accordion" id="logs">';
+
+ if (equal($current_page,0))
+ {
+ append($html,html_entity_decode($this->execute("git log -n 100 --pretty=format:'$format'")->join('')));
+ }else{
+ $x = 100 * $current_page;
+ append($html,html_entity_decode($this->execute("git log --skip=$x -n 100 --pretty=format:'$format'")->join('')));
+ }
+
+
+ append($html,'</div>','<div class="container">',$pagination,'</div>');
+ return $html;
+ }
+
+ /**
+ *
+ * Get the current branch
+ *
+ * @return string
+ *
+ */
+ public function current_branch(): string
+ {
+ return $this->execute('git branch --show-current')->get(0);
+ }
+ /**
+ *
+ * Execute a command an store result
+ *
+ * @param string $command
+ *
+ * @return Collect
+ *
+ */
+ private function execute(string $command): Collect
+ {
+ $this->data = [];
+ exec($command,$this->data);
+ return collect($this->data);
+ }
+
+
} \ No newline at end of file
diff --git a/tests/FormTest.php b/tests/FormTest.php
index 0164439..50ad34c 100644
--- a/tests/FormTest.php
+++ b/tests/FormTest.php
@@ -42,7 +42,7 @@ class FormTest extends Unit
*/
public function test_add()
{
- $form = $this->form->add('username','text')->add('email','email')->get();
+ $form = $this->form->add('username','text')->add('username','text')->add('email','email')->get();
$this->assertStringContainsString('username',$form);
$this->assertStringContainsString('email',$form);
}
diff --git a/tests/Versioning/GitTest.php b/tests/Versioning/GitTest.php
new file mode 100644
index 0000000..b69f99b
--- /dev/null
+++ b/tests/Versioning/GitTest.php
@@ -0,0 +1,78 @@
+<?php
+
+
+namespace Testing\Versioning;
+
+
+use DI\DependencyException;
+use DI\NotFoundException;
+use Imperium\Testing\Unit;
+use Imperium\Versioning\Git;
+
+class GitTest extends Unit
+{
+ /**
+ * @var Git
+ */
+ private $git;
+
+ /**
+ * @throws DependencyException
+ * @throws NotFoundException
+ */
+ public function setUp(): void
+ {
+ $this->git = $this->git('.','willy');
+ }
+
+ public function test_base()
+ {
+ $this->assertEquals('imperium',$this->git->name());
+ $this->assertEquals('willy',$this->git->owner());
+ }
+
+ public function test_status()
+ {
+ $this->assertNotEmpty($this->git->status());
+ }
+ public function test_diff()
+ {
+ $this->assertNotEmpty($this->git->diff());
+ $this->assertNotEmpty($this->git->diff('10.5.2','10.5.3'));
+ }
+ public function test_branch()
+ {
+ $this->assertEquals('develop',$this->git->current_branch());
+ }
+ public function test_commit_size()
+ {
+ $a = $this->git->commits_size('master');
+ $b = $this->git->commits_size('develop');
+
+ $this->assertIsInt($a);
+ $this->assertIsInt($b);
+ $this->assertNotEquals($a,$b);
+ }
+ public function test_branches()
+ {
+ $branches = $this->git->branches();
+ $this->assertContains('master',$branches);
+ $this->assertContains('develop',$branches);
+
+ }
+
+ public function test_log()
+ {
+ $first = $this->git->log(0);
+ $second = $this->git->log(1);
+ $this->assertNotEmpty($first);
+ $this->assertNotEmpty($second);
+ $this->assertTrue(different($first,$second));
+ }
+
+ public function test_releases()
+ {
+ $this->assertNotEmpty($this->git->releases());
+ }
+
+} \ No newline at end of file