Skip to content

Commit fd4281a

Browse files
committed
split Git\Index to Git\IndexIterator,Git\Index.
- Git\Index removed Iterator interface, added IteratorAggregate interface. - Added Git\IndexIterator class. foreach($index->getIterator() as $entry){ //something do it } [removed] - Git\Index::current() - Git\Index::next() - Git\Index::valid() - Git\Index::rewind() - Git\Index::key() [added] - Git\IndexIterator::current() - Git\IndexIterator::next() - Git\IndexIterator::valid() - Git\IndexIterator::rewind() - Git\IndexIterator::key()
1 parent d4f3c30 commit fd4281a

File tree

7 files changed

+172
-59
lines changed

7 files changed

+172
-59
lines changed

src/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if test $PHP_GIT != "no"; then
2929
PHP_ADD_LIBRARY_WITH_PATH(git2, $LIBGIT2_LIBDIR, GIT_SHARED_LIBADD)
3030

3131
PHP_SUBST(GIT_SHARED_LIBADD)
32-
PHP_NEW_EXTENSION(git,php_git.c reference.c repository.c signature.c commit.c index_entry.c index.c tree.c blob.c tree_entry.c walker.c object.c rawobject.c tag.c odb.c backend.c, $ext_shared)
32+
PHP_NEW_EXTENSION(git,php_git.c reference.c repository.c signature.c commit.c index_entry.c index_iterator.c index.c tree.c blob.c tree_entry.c walker.c object.c rawobject.c tag.c odb.c backend.c, $ext_shared)
3333

3434
ifdef([PHP_ADD_EXTENSION_DEP],
3535
[

src/index.c

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <time.h>
3030

3131
PHPAPI zend_class_entry *git_index_class_entry;
32+
PHPAPI zend_class_entry *git_index_iterator_class_entry;
3233

3334
static void php_git_index_free_storage(php_git_index_t *obj TSRMLS_DC)
3435
{
@@ -109,52 +110,6 @@ void php_git_index_entry_create(zval **index, git_index_entry *entry)
109110
add_property_long(*index,"mtime",time(&entry->mtime.seconds));
110111
}
111112

112-
PHP_METHOD(git_index, current)
113-
{
114-
php_git_index_t *this = (php_git_index_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
115-
git_index_entry *entry;
116-
zval *git_index_entry;
117-
118-
entry = git_index_get(this->index,this->offset);
119-
if(entry == NULL){
120-
return;
121-
}
122-
php_git_index_entry_create(&git_index_entry, entry);
123-
RETURN_ZVAL(git_index_entry,0,0);
124-
}
125-
126-
PHP_METHOD(git_index, key)
127-
{
128-
php_git_index_t *this = (php_git_index_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
129-
RETURN_LONG(this->offset);
130-
}
131-
132-
PHP_METHOD(git_index, next)
133-
{
134-
php_git_index_t *this = (php_git_index_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
135-
this->offset++;
136-
137-
RETURN_TRUE;
138-
}
139-
140-
PHP_METHOD(git_index, rewind)
141-
{
142-
php_git_index_t *this = (php_git_index_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
143-
this->offset = 0;
144-
}
145-
146-
PHP_METHOD(git_index, valid)
147-
{
148-
php_git_index_t *this = (php_git_index_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
149-
int entry_count = git_index_entrycount(this->index);
150-
151-
if(this->offset < entry_count && this->offset >= 0){
152-
RETURN_TRUE;
153-
}else{
154-
RETURN_FALSE;
155-
}
156-
}
157-
158113
PHP_METHOD(git_index, count)
159114
{
160115
php_git_index_t *this = (php_git_index_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
@@ -284,6 +239,19 @@ PHP_METHOD(git_index, refresh)
284239

285240
git_index_read(index);
286241
}
242+
243+
PHP_METHOD(git_index, getIterator)
244+
{
245+
php_git_index_t *this = (php_git_index_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
246+
zval *iterator;
247+
248+
MAKE_STD_ZVAL(iterator);
249+
object_init_ex(iterator,git_index_iterator_class_entry);
250+
php_git_index_iterator_t *obj = (php_git_index_iterator_t *) zend_object_store_get_object(iterator TSRMLS_CC);
251+
obj->index = this->index;
252+
obj->offset = 0;
253+
RETURN_ZVAL(iterator,0,0);
254+
}
287255
/*
288256
PHP_METHOD(git_index, __construct)
289257
{
@@ -350,12 +318,7 @@ PHPAPI function_entry php_git_index_methods[] = {
350318
//PHP_ME(git_index, insert, arginfo_git_index_insert, ZEND_ACC_PUBLIC)
351319
// Countable
352320
PHP_ME(git_index, count, NULL, ZEND_ACC_PUBLIC)
353-
// Iterator
354-
PHP_ME(git_index, current, NULL, ZEND_ACC_PUBLIC)
355-
PHP_ME(git_index, key, NULL, ZEND_ACC_PUBLIC)
356-
PHP_ME(git_index, next, NULL, ZEND_ACC_PUBLIC)
357-
PHP_ME(git_index, rewind, NULL, ZEND_ACC_PUBLIC)
358-
PHP_ME(git_index, valid, NULL, ZEND_ACC_PUBLIC)
321+
PHP_ME(git_index, getIterator, NULL, ZEND_ACC_PUBLIC)
359322
{NULL, NULL, NULL}
360323
};
361324

@@ -366,5 +329,5 @@ void git_index_init(TSRMLS_D)
366329

367330
git_index_class_entry = zend_register_internal_class(&git_index_ce TSRMLS_CC);
368331
git_index_class_entry->create_object = php_git_index_new;
369-
zend_class_implements(git_index_class_entry TSRMLS_CC, 2, spl_ce_Countable, spl_ce_Iterator);
332+
zend_class_implements(git_index_class_entry TSRMLS_CC, 2, spl_ce_Countable, zend_ce_aggregate);
370333
}

src/index_iterator.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2010 - 2011 Shuhei Tanuma
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "php_git.h"
26+
#include <spl/spl_array.h>
27+
#include <zend_interfaces.h>
28+
#include <string.h>
29+
#include <time.h>
30+
31+
32+
PHPAPI zend_class_entry *git_index_iterator_class_entry;
33+
extern void php_git_index_entry_create(zval **index, git_index_entry *entry);
34+
35+
static void php_git_index_iterator_free_storage(php_git_index_iterator_t *obj TSRMLS_DC)
36+
{
37+
zend_object_std_dtor(&obj->zo TSRMLS_CC);
38+
obj->index = NULL;
39+
efree(obj);
40+
}
41+
42+
zend_object_value php_git_index_iterator_new(zend_class_entry *ce TSRMLS_DC)
43+
{
44+
zend_object_value retval;
45+
php_git_index_iterator_t *obj;
46+
zval *tmp;
47+
48+
obj = ecalloc(1, sizeof(*obj));
49+
zend_object_std_init( &obj->zo, ce TSRMLS_CC );
50+
zend_hash_copy(obj->zo.properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
51+
52+
retval.handle = zend_objects_store_put(obj,
53+
(zend_objects_store_dtor_t)zend_objects_destroy_object,
54+
(zend_objects_free_object_storage_t)php_git_index_iterator_free_storage,
55+
NULL TSRMLS_CC);
56+
retval.handlers = zend_get_std_object_handlers();
57+
return retval;
58+
}
59+
60+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_index_iterator__construct, 0, 0, 1)
61+
ZEND_ARG_INFO(0, index)
62+
ZEND_END_ARG_INFO()
63+
64+
PHP_METHOD(git_index_iterator, current)
65+
{
66+
php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
67+
git_index_entry *entry;
68+
zval *git_index_entry;
69+
70+
entry = git_index_get(this->index,this->offset);
71+
if(entry == NULL){
72+
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
73+
"specified offset does not exist. %d");
74+
RETURN_FALSE;
75+
}
76+
php_git_index_entry_create(&git_index_entry, entry);
77+
RETURN_ZVAL(git_index_entry,0,0);
78+
}
79+
80+
PHP_METHOD(git_index_iterator, key)
81+
{
82+
php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
83+
RETURN_LONG(this->offset);
84+
}
85+
86+
PHP_METHOD(git_index_iterator, next)
87+
{
88+
php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
89+
this->offset++;
90+
91+
RETURN_TRUE;
92+
}
93+
94+
PHP_METHOD(git_index_iterator, rewind)
95+
{
96+
php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
97+
this->offset = 0;
98+
}
99+
100+
PHP_METHOD(git_index_iterator, valid)
101+
{
102+
php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
103+
int entry_count = git_index_entrycount(this->index);
104+
if(this->offset < entry_count && this->offset >= 0){
105+
RETURN_TRUE;
106+
}else{
107+
RETURN_FALSE;
108+
}
109+
}
110+
111+
PHP_METHOD(git_index_iterator, __construct)
112+
{
113+
php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC);
114+
zval *php_git_index;
115+
116+
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
117+
"z", &php_git_index) == FAILURE){
118+
return;
119+
}
120+
php_git_index_t *idx = (php_git_index_t *) zend_object_store_get_object(php_git_index TSRMLS_CC);
121+
122+
this->index = idx->index;
123+
this->offset = 0;
124+
}
125+
126+
PHPAPI function_entry php_git_index_iterator_methods[] = {
127+
PHP_ME(git_index_iterator, __construct, arginfo_git_index_iterator__construct,ZEND_ACC_PUBLIC)
128+
PHP_ME(git_index_iterator, current, NULL, ZEND_ACC_PUBLIC)
129+
PHP_ME(git_index_iterator, key, NULL, ZEND_ACC_PUBLIC)
130+
PHP_ME(git_index_iterator, next, NULL, ZEND_ACC_PUBLIC)
131+
PHP_ME(git_index_iterator, rewind, NULL, ZEND_ACC_PUBLIC)
132+
PHP_ME(git_index_iterator, valid, NULL, ZEND_ACC_PUBLIC)
133+
};
134+
135+
void git_index_iterator_init(TSRMLS_D)
136+
{
137+
zend_class_entry git_index_iterator_ce;
138+
INIT_NS_CLASS_ENTRY(git_index_iterator_ce, PHP_GIT_NS,"IndexIterator", php_git_index_iterator_methods);
139+
git_index_iterator_class_entry = zend_register_internal_class(&git_index_iterator_ce TSRMLS_CC);
140+
git_index_iterator_class_entry->create_object = php_git_index_iterator_new;
141+
zend_class_implements(git_index_iterator_class_entry TSRMLS_CC, 1, spl_ce_Iterator);
142+
}

src/php_git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ PHP_MINIT_FUNCTION(git) {
133133
git_init_object(TSRMLS_C);
134134
git_init_reference(TSRMLS_C);
135135
php_git_repository_init(TSRMLS_C);
136+
git_index_iterator_init(TSRMLS_C);
136137
git_index_entry_init(TSRMLS_C);
137138
git_index_init(TSRMLS_C);
138139
git_init_signature(TSRMLS_C);

src/php_git.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern PHPAPI zend_class_entry *git_reference_class_entry;
5050
extern PHPAPI zend_class_entry *git_repository_class_entry;
5151
extern PHPAPI zend_class_entry *git_object_class_entry;
5252
extern PHPAPI zend_class_entry *git_index_class_entry;
53+
extern PHPAPI zend_class_entry *git_index_iterator_class_entry;
5354
extern PHPAPI zend_class_entry *git_index_entry_class_entry;
5455
extern PHPAPI zend_class_entry *git_walker_class_entry;
5556
extern PHPAPI zend_class_entry *git_tree_class_entry;
@@ -65,9 +66,14 @@ extern PHPAPI zend_class_entry *git_backend_class_entry;
6566
typedef struct{
6667
zend_object zo;
6768
git_index *index;
68-
long offset;
6969
} php_git_index_t;
7070

71+
typedef struct{
72+
zend_object zo;
73+
git_index *index;
74+
long offset;
75+
} php_git_index_iterator_t;
76+
7177
typedef struct{
7278
zend_object zo;
7379
git_repository *repository;

src/repository.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ PHP_METHOD(git_repository, getIndex)
158158
git_repository *repository;
159159
git_index *index;
160160

161-
zval *index_object = emalloc(sizeof(zval));
161+
zval *index_object;
162162
int ret = 0;
163163

164164
php_git_repository_t *myobj = (php_git_repository_t *) zend_object_store_get_object(object TSRMLS_CC);
@@ -173,17 +173,17 @@ PHP_METHOD(git_repository, getIndex)
173173
RETURN_FALSE;
174174
}
175175

176+
MAKE_STD_ZVAL(index_object);
176177
object_init_ex(index_object, git_index_class_entry);
177178
php_git_index_t *iobj = (php_git_index_t *) zend_object_store_get_object(index_object TSRMLS_CC);
178179

179180
iobj->index = index;
180-
iobj->offset = 0;
181181

182182
git_index_read(index);
183183

184184
//Todo: Read from Git object.
185185
//add_property_string_ex(index_object, "path",5,index->index_file_path, 1 TSRMLS_CC);
186-
add_property_long(index_object, "entry_count",git_index_entrycount(index));
186+
//add_property_long(index_object, "entry_count",git_index_entrycount(index));
187187

188188
RETURN_ZVAL(index_object,0,0);
189189
}

tests/GitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ public function testConstruct()
104104

105105
public function testGetIndex()
106106
{
107+
107108
$git = new Git\Repository(dirname(__DIR__) . "/.git/");
108109
$index = $git->getIndex();
109110
if($index instanceof Git\Index){
110-
foreach($index as $entry){
111+
foreach($index->getIterator() as $entry){
111112
$this->assertInstanceof("Git\\Index\\Entry",$entry);
112113
}
113114
return true;

0 commit comments

Comments
 (0)