Skip to content

Commit 7ec7717

Browse files
authored
feat(appengine): add custom extension sample (GoogleCloudPlatform#1372)
1 parent 7ea14e3 commit 7ec7717

File tree

10 files changed

+182
-0
lines changed

10 files changed

+182
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud Platform
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore
15+
16+
# PHP Composer dependencies:
17+
/vendor/
18+
19+
# Files from phpize
20+
ext/Makefile.global
21+
ext/acinclude.m4
22+
ext/aclocal.m4
23+
ext/autom4te.cache/
24+
ext/config.guess
25+
ext/config.h.in
26+
ext/config.sub
27+
ext/configure
28+
ext/configure.ac
29+
ext/install-sh
30+
ext/ltmain.sh
31+
ext/missing
32+
ext/mkinstalldirs
33+
ext/run-tests.php
34+
35+
# Files from ./configure
36+
ext/Makefile
37+
ext/Makefile.fragments
38+
ext/Makefile.objects
39+
ext/config.h
40+
ext/config.log
41+
ext/config.nice
42+
ext/config.status
43+
ext/libtool
44+
45+
# Files from make
46+
ext/.libs/
47+
ext/modules/
48+
ext/*.la
49+
ext/*.lo
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Files from phpize
2+
ext/Makefile.global
3+
ext/acinclude.m4
4+
ext/aclocal.m4
5+
ext/autom4te.cache/
6+
ext/config.guess
7+
ext/config.h.in
8+
ext/config.sub
9+
ext/configure
10+
ext/configure.ac
11+
ext/install-sh
12+
ext/ltmain.sh
13+
ext/missing
14+
ext/mkinstalldirs
15+
ext/run-tests.php
16+
17+
# Files from ./configure
18+
ext/Makefile
19+
ext/Makefile.fragments
20+
ext/Makefile.objects
21+
ext/config.h
22+
ext/config.log
23+
ext/config.nice
24+
ext/config.status
25+
ext/libtool
26+
27+
# Files from make
28+
ext/.libs/
29+
ext/modules/
30+
ext/*.la
31+
ext/*.lo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runtime: php74
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"scripts": {
3+
"post-autoload-dump": [
4+
"cd ext && phpize --clean && phpize && ./configure && make",
5+
"cp ext/modules/my_custom_extension.so vendor/"
6+
]
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PHP_ARG_ENABLE(my_custom_extension, Whether to enable the MyCustomExtension extension, [ --enable-my-custom-extension Enable MyCustomExtension])
2+
3+
if test "$MY_CUSTOM_EXTENSION" != "no"; then
4+
PHP_NEW_EXTENSION(my_custom_extension, my_custom_extension.c, $ext_shared)
5+
fi
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// include the PHP API itself
2+
#include
3+
// include the extension header
4+
#include "my_custom_extension.h"
5+
6+
// register the "helloworld_from_extension" function to the PHP API
7+
zend_function_entry my_custom_extension_functions[] = {
8+
PHP_FE(helloworld_from_extension, NULL)
9+
{NULL, NULL, NULL}
10+
};
11+
12+
// some information about our module
13+
zend_module_entry my_custom_extension_module_entry = {
14+
STANDARD_MODULE_HEADER,
15+
PHP_MY_CUSTOM_EXTENSION_EXTNAME,
16+
my_custom_extension_functions,
17+
NULL,
18+
NULL,
19+
NULL,
20+
NULL,
21+
NULL,
22+
PHP_MY_CUSTOM_EXTENSION_VERSION,
23+
STANDARD_MODULE_PROPERTIES
24+
};
25+
26+
// use a macro to output additional C code, to make ext dynamically loadable
27+
ZEND_GET_MODULE(my_custom_extension)
28+
29+
// Implement our "Hello World" function, which returns a string
30+
PHP_FUNCTION(helloworld_from_extension) {
31+
zval val;
32+
ZVAL_STRING(&val, "Hello World! (from my_custom_extension.so)\n");
33+
RETURN_STR(Z_STR(val));
34+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// module constants
2+
#define PHP_MY_CUSTOM_EXTENSION_EXTNAME "my_custom_extension"
3+
#define PHP_MY_CUSTOM_EXTENSION_VERSION "0.0.1"
4+
5+
// the function to be exported
6+
PHP_FUNCTION(helloworld_from_extension);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
echo helloworld_from_extension();

appengine/standard/extensions/php.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extension=/workspace/vendor/my_custom_extension.so
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Google\Cloud\Samples\AppEngine\HelloWorld;
18+
19+
use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* @group deploy
24+
*/
25+
class DeployTest extends TestCase
26+
{
27+
use AppEngineDeploymentTrait;
28+
29+
public function testIndex()
30+
{
31+
$resp = $this->client->get('/');
32+
33+
$this->assertEquals(
34+
'200',
35+
$resp->getStatusCode(),
36+
'Top page status code should be 200'
37+
);
38+
39+
$this->assertStringContainsString(
40+
'Hello World! (from my_custom_extension.so)',
41+
(string) $resp->getBody()
42+
);
43+
}
44+
}

0 commit comments

Comments
 (0)