Skip to content

Commit f72f89d

Browse files
Johannes HahnJohannes Hahn
Johannes Hahn
authored and
Johannes Hahn
committed
init repo php-libgit2
1 parent d39da1d commit f72f89d

12 files changed

+576
-0
lines changed

.gitignore

Whitespace-only changes.

Makefile

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
# Makefile template
3+
#
4+
# This is an example Makefile that can be used by anyone who is building
5+
# his or her own PHP extensions using the PHP-CPP library.
6+
#
7+
# In the top part of this file we have included variables that can be
8+
# altered to fit your configuration, near the bottom the instructions and
9+
# dependencies for the compiler are defined. The deeper you get into this
10+
# file, the less likely it is that you will have to change anything in it.
11+
#
12+
13+
#
14+
# Name of your extension
15+
#
16+
# This is the name of your extension. Based on this extension name, the
17+
# name of the library file (name.so) and the name of the config file (name.ini)
18+
# are automatically generated
19+
#
20+
21+
NAME = php-libgit2
22+
23+
24+
#
25+
# Php.ini directories
26+
#
27+
# In the past, PHP used a single php.ini configuration file. Today, most
28+
# PHP installations use a conf.d directory that holds a set of config files,
29+
# one for each extension. Use this variable to specify this directory.
30+
#
31+
32+
INI_DIR = /etc/php5/conf.d
33+
34+
35+
#
36+
# The extension dirs
37+
#
38+
# This is normally a directory like /usr/lib/php5/20121221 (based on the
39+
# PHP version that you use. We make use of the command line 'php-config'
40+
# instruction to find out what the extension directory is, you can override
41+
# this with a different fixed directory
42+
#
43+
44+
EXTENSION_DIR = $(shell php-config --extension-dir)
45+
46+
47+
#
48+
# The name of the extension and the name of the .ini file
49+
#
50+
# These two variables are based on the name of the extension. We simply add
51+
# a certain extension to them (.so or .ini)
52+
#
53+
54+
EXTENSION = ${NAME}.so
55+
INI = ${NAME}.ini
56+
57+
58+
#
59+
# Compiler
60+
#
61+
# By default, the GNU C++ compiler is used. If you want to use a different
62+
# compiler, you can change that here. You can change this for both the
63+
# compiler (the program that turns the c++ files into object files) and for
64+
# the linker (the program that links all object files into the single .so
65+
# library file. By default, g++ (the GNU C++ compiler) is used for both.
66+
#
67+
68+
COMPILER = g++
69+
LINKER = g++
70+
71+
72+
#
73+
# Compiler and linker flags
74+
#
75+
# This variable holds the flags that are passed to the compiler. By default,
76+
# we include the -O2 flag. This flag tells the compiler to optimize the code,
77+
# but it makes debugging more difficult. So if you're debugging your application,
78+
# you probably want to remove this -O2 flag. At the same time, you can then
79+
# add the -g flag to instruct the compiler to include debug information in
80+
# the library (but this will make the final libphpcpp.so file much bigger, so
81+
# you want to leave that flag out on production servers).
82+
#
83+
# If your extension depends on other libraries (and it does at least depend on
84+
# one: the PHP-CPP library), you should update the LINKER_DEPENDENCIES variable
85+
# with a list of all flags that should be passed to the linker.
86+
#
87+
88+
COMPILER_FLAGS = -Wall -c -O2 -std=c++11 -fpic -o
89+
LINKER_FLAGS = -shared
90+
LINKER_DEPENDENCIES = -lphpcpp -lgit2
91+
92+
93+
#
94+
# Command to remove files, copy files and create directories.
95+
#
96+
# I've never encountered a *nix environment in which these commands do not work.
97+
# So you can probably leave this as it is
98+
#
99+
100+
RM = rm -f
101+
CP = cp -f
102+
MKDIR = mkdir -p
103+
104+
105+
#
106+
# All source files are simply all *.cpp files found in the current directory
107+
#
108+
# A builtin Makefile macro is used to scan the current directory and find
109+
# all source files. The object files are all compiled versions of the source
110+
# file, with the .cpp extension being replaced by .o.
111+
#
112+
113+
SOURCES = $(wildcard *.cpp)
114+
OBJECTS = $(SOURCES:%.cpp=%.o)
115+
116+
117+
#
118+
# From here the build instructions start
119+
#
120+
121+
all: ${OBJECTS} ${EXTENSION}
122+
123+
${EXTENSION}: ${OBJECTS}
124+
${LINKER} ${LINKER_FLAGS} -o $@ ${OBJECTS} ${LINKER_DEPENDENCIES}
125+
126+
${OBJECTS}:
127+
${COMPILER} ${COMPILER_FLAGS} $@ ${@:%.o=%.cpp}
128+
129+
install:
130+
${CP} ${EXTENSION} ${EXTENSION_DIR}
131+
${CP} ${INI} ${INI_DIR}
132+
133+
clean:
134+
${RM} ${EXTENSION} ${OBJECTS}
135+

common.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// common.cpp
3+
// php-libgit2
4+
//
5+
// Created by Johannes Hahn on 01.06.15.
6+
// Copyright (c) 2015 Johannes Hahn. All rights reserved.
7+
//
8+
9+
#include "common.h"
10+
11+
git_repository *_git_repository = NULL;
12+
13+
const char* NO_GIT_REPOSITORY = "No git repository loaded";
14+
const int DEBUG_LIBGIT2 = 0;
15+
16+
int check_lg2(int error)
17+
{
18+
19+
if (error < 0) {
20+
if(DEBUG_LIBGIT2 == 1){
21+
const git_error *e = giterr_last();
22+
printf("Error %d/%d: %s\n", error, e->klass, e->message);
23+
}
24+
return 0;
25+
}
26+
return 1;
27+
}
28+
29+

common.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// common.h
3+
// php-libgit2
4+
//
5+
// Created by Johannes Hahn on 01.06.15.
6+
// Copyright (c) 2015 Johannes Hahn. All rights reserved.
7+
//
8+
9+
#include <stdio.h>
10+
#include <string.h>
11+
#include <stdlib.h>
12+
#include <iostream>
13+
14+
#include "git2.h"
15+
#include "phpcpp.h"
16+
17+
18+
/**
19+
* Check libgit2 error code, printing error to stderr on failure and
20+
* exiting the program.
21+
*/
22+
23+
24+
extern const char* NO_GIT_REPOSITORY;
25+
extern const int DEBUG_LIBGIT2;
26+
27+
extern int check_lg2(int error);
28+
extern git_repository *_git_repository;

main.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// main.cpp
3+
// php-libgit2
4+
//
5+
// Created by Johannes Hahn on 01.06.15.
6+
// Copyright (c) 2015 Johannes Hahn. All rights reserved.
7+
//
8+
9+
#include "main.h"
10+
#include "repository.h"
11+
#include "status.h"
12+
13+
14+
15+
/**
16+
* tell the compiler that the get_module is a pure C function
17+
*/
18+
extern "C" {
19+
20+
/**
21+
* Function that is called by PHP right after the PHP process
22+
* has started, and that returns an address of an internal PHP
23+
* strucure with all the details and features of your extension
24+
*
25+
* @return void* a pointer to an address that is understood by PHP
26+
*/
27+
PHPCPP_EXPORT void *get_module()
28+
{
29+
30+
static Php::Extension extension_phpLibgit2("php-libgit2", "1.0");
31+
Php::Namespace namepsace_PhpLibgit2("PhpLibgit2");
32+
33+
34+
35+
/* ############################################################################################################### */
36+
/* Repository to PHP - start */
37+
38+
Php::Class<PhpLibgit2::Repository> class_Repository("Repository");
39+
40+
class_Repository.method("getPath", &PhpLibgit2::Repository::getPath);
41+
class_Repository.method("getIsBare", &PhpLibgit2::Repository::getIsBare);
42+
43+
class_Repository.method("git_repository_init",
44+
&PhpLibgit2::Repository::php_git_repository_init,
45+
Php::Public,{
46+
Php::ByVal("path", Php::Type::String, true),
47+
Php::ByVal("is_bare", Php::Type::Bool, false)
48+
});
49+
50+
class_Repository.method("git_repository_open",
51+
&PhpLibgit2::Repository::php_git_repository_open,
52+
Php::Public, {
53+
Php::ByVal("path", Php::Type::String, false)
54+
});
55+
56+
class_Repository.method("git_repository_open_bare",
57+
&PhpLibgit2::Repository::php_git_repository_open_bare,
58+
Php::Public, {
59+
Php::ByVal("path", Php::Type::String, false)
60+
});
61+
62+
class_Repository.method("git_repository_is_bare", &PhpLibgit2::Repository::php_git_repository_is_bare, Php::Public);
63+
64+
65+
namepsace_PhpLibgit2.add(std::move(class_Repository));
66+
67+
/* Repository to PHP - end */
68+
/* ############################################################################################################### */
69+
70+
71+
/* ############################################################################################################### */
72+
/* Status to PHP - start */
73+
74+
Php::Class<PhpLibgit2::Status> class_Status("Status");
75+
76+
class_Status.method("git_status_list_new", &PhpLibgit2::Status::php_git_status_list_new);
77+
class_Status.method("git_status_list_entrycount", &PhpLibgit2::Status::php_git_status_list_entrycount);
78+
79+
80+
namepsace_PhpLibgit2.add(std::move(class_Status));
81+
82+
/* Status to PHP - end */
83+
/* ############################################################################################################### */
84+
85+
86+
// add the namespace to the extension
87+
extension_phpLibgit2.add(std::move(namepsace_PhpLibgit2));
88+
// return the extension
89+
return extension_phpLibgit2;
90+
}
91+
}

main.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// main.h
3+
// php-libgit2
4+
//
5+
// Created by Johannes Hahn on 04.06.15.
6+
// Copyright (c) 2015 Johannes Hahn. All rights reserved.
7+
//
8+
9+
#ifndef php_libgit2_main_h
10+
#define php_libgit2_main_h
11+
12+
13+
14+
#endif

php-libgit2.ini

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extension=[PATH TO FILE edit this]/php-libgit2.so

0 commit comments

Comments
 (0)