shpasser / gae-support-l5
Google App Engine Support for Laravel 5.1 apps.
Installs: 15 385
Dependents: 0
Suggesters: 0
Security: 0
Stars: 160
Watchers: 14
Forks: 27
Open Issues: 4
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
- league/flysystem: ~1.0
Requires (Dev)
- illuminate/console: ~5.0
- phpunit/phpunit: ~4.4
README
Google App Engine(GAE) Support package for Laravel 5.1.
Currently supported features:
- Generation of general configuration files
- Mail service provider
- Queue service provider
- Database connection
- Filesystem
For Lumen see https://github.com/shpasser/GaeSupportLumen.
Installation
Pull in the package via Composer.
"require": { "shpasser/gae-support-l5": "~1.0" }
Then include the service provider within config/app.php
.
'providers' => [ Shpasser\GaeSupportL5\GaeSupportServiceProvider::class ];
Usage
Generate the GAE related files/entries.
Command template:
php artisan gae:setup --config --cache-config --bucket="your-bucket-id" --db-socket="cloud-sql-instance-socket-connection-string" --db-name="cloud-sql-database-name" --db-host="cloud-sql-instance-ipv4-address" app-id
Arguments and Options:
php artisan gae:setup [options] [--] app-id Arguments: app-id GAE application ID. Options: --config Generate "app.yaml" and "php.ini" config files. --cache-config Generate cached Laravel config file for use on Google App Engine. --bucket=BUCKET Use the specified GCS-bucket instead of the default one. --db-socket=DB-SOCKET Cloud SQL socket connection string for production environment. --db-name=DB-NAME Cloud SQL database name. --db-host=DB-HOST Cloud SQL database host IPv4 address for local environment.
--cache-config
option generates cached config file for GAE. This option is essential, because cached config file generated by php artisan config:cache
is not suitable for use on GAE. As well, cached config file generated for GAE probably will not work in local environment. This option should be used to generate cached config file before application is deployed on GAE.
--bucket
option defines the GCS-bucket ID to be used by the application for storage. Default GCS bucket is configured unless the option is used.
When --db-name
option is defined at least one of --db-socket
or --db-host
should
be also defined.
--db-socket
is set using the following format: /cloudsql/<app-id>:<cloud-sql-instance-name>
. Where <cloud-sql-instance-name>
is the Cloud SQL instance name and <app-id>
is the name of the application it belongs to.
The mail driver configuration can be found in config/mail.php
and .env.production
,
these configuration files are modified / generated by the artisan command. There is
no need in any kind of custom configuration. All the outgoing mail messages are sent
with sender address of an administrator of the application, i.e. admin@your-app-id.appspotmail.com
.
The sender
, to
, cc
, bcc
, replyTo
, subject
, body
and attachment
parts of email message are supported.
Queues
The modified queue configuration file config/queue.php
should contain:
return array( ... /* |-------------------------------------------------------------------------- | GAE Queue Connection |-------------------------------------------------------------------------- | */ 'connections' => array( 'gae' => array( 'driver' => 'gae', 'queue' => 'default', 'url' => '/tasks', 'encrypt' => true, ), ... ), );
The 'default' queue and encryption are used by default.
In order to use the queue your app/Http/routes.php
file should contain the following route:
Route::post('tasks', array('as' => 'tasks', function() { return Queue::marshal(); }));
This route will be used by the GAE queue to push the jobs. Please notice that the route and the GAE Queue Connection 'url' parameter point to the same URL. Since the requests submitted using the route are issued by GAE itself it cannot be CSRF-protected. For more information on the matter please see http://laravel.com/docs/5.0/queues#push-queues.
Cache, Session and Log
Cache, Session and Log components are supported via the use of specific drivers / handlers:
- Cache - using the 'memcached' driver,
- Session - using the 'memcached' driver,
- Log - using 'syslog' handler.
The configuration options for the mentioned drivers / handlers are generated by the artisan command
and can be found in .env.production
configuration file.
Database
Google Cloud SQL is supported via Laravel's MySql driver. The connection configuration is added by
the artisan command to config/database.php
under cloudsql
. The connection parameters can be
configured using --db-socket
, --db-name
and --db-host
options via the artisan command.
The database related environment variables are set in .env.production
and .env.local
files.
The production
environment is configured to use the socket connection while the local
configured
to connect via the IPv4 address of the Google Cloud SQL instance. Use Google Developers Console in
order to obtain the socket connection string and enable the IPv4 address of your database instance.
The migrations are supported while working in local
environment only.
To use either the production
or the local
environment rename the appropriate file to .env
.
Filesystem
In order to support Laravel filesystem on GAE the artisan command modifies config/filesystem.php
to include an additional disk:
'gae' => [ 'driver' => 'gae', 'root' => storage_path().'/app', ],
and adds the following line to .env.production
file:
FILESYSTEM = gae
Optimizations
The optimizations allow the application to reduce the use of GCS, which is the only read-write storage available on GAE platform as of now.
In order to optimize view compilation the included cachefs
filesystem can be used to store
compiled views using memcached
service. cachefs
does not provide the application with a
reliable storage solution, information stored using memcached
is managed according to
memcached
rules and may be deleted when memcached
decides to. Since the views can
be compiled again without any information loss it is appropriate to store compiled
views using cachefs
.
cachefs
has the following structure:
/ +-- bootstrap +-- cache +-- framework +-- views
'/framework/views' is used to store the compiled views.
Use the following option to enable the feature in .env.production
and/or .env.local
file:
CACHE_COMPILED_VIEWS = true
'/bootstrap/cache' is used to store the services.json
, config.php
and routes.php
files,
in order to control caching of these files use the following options in .env.production
and/or .env.local
file:
CACHE_SERVICES_FILE = true CACHE_CONFIG_FILE = true CACHE_ROUTES_FILE = true
In order to use config.php
first generate it using the --cache-config
option of
php artisan gae:setup
command. routes.php
has to be generated using
php artisan route:cache
command.
Cache related options are:
- supported on GAE and/or in local environment as long as
memcached
service is present, - disabled while executing
php artisan gae:setup
command.
Additionally the initialization of GSC bucket can be skipped to boost the performance.
In order to do so, set the following option in the app.yaml
file:
env_variables: GAE_SKIP_GCS_INIT: true
the storage path will be set to /storage
directory of the GCS bucket and storage
directory structure creation will be skipped.
If not used the filesystem initialization can be removed to minimize GCS usage. In order to
do so, remove the following line from .env.production
file:
FILESYSTEM = gae
Artisan Console for GAE
To support artisan
commands while running on GAE the package provides Artisan Console for GAE
.
The console is implemented as a separate service and not enabled by default for security reasons. To use the console securely /artisan
route has to be protected.
Installation
Include the service provider within config/app.php
.
'providers' => [ Shpasser\GaeSupportL5\GaeArtisanConsoleServiceProvider::class ];
Add /artisan
URL handler to app.yaml
file.
handlers: - url: /artisan script: public/index.php login: admin secure: always - url: /.* script: public/index.php
/artisan
URL handler has to appear before the last one (url: /.*
), otherwise it will be ignored by GAE.
The suggested handler secures the route using GAE URL security options. For more information see https://cloud.google.com/appengine/docs/php/config/appconfig#PHP_app_yaml_Secure_URLs.
Usage
Enter URL http://your-app-id.appspot.com/artisan in your browser and use the displayed form to submit artisan
commands.
Since GAE's filesystem is read-only the commands will not be able to perform write / update operations on it. For the same reason migrations have to be prepared on local development environment before the deployment takes place. Since the console is not really interactive all the commands are executed in non-interactive mode(by automatic appending of -n
option).
Deployment
Backup the existing .env
file if needed and rename the generated .env.production
to .env
before deploying your app.
Download and install GAE SDK for PHP and deploy your app.
Known Issues
As of now Laravel scheduled commands are not supported while running on GAE.
In order to use Artisan Console for GAE
the application class app/Console/Kernel
has be edited and any of the commands scheduled using its schedule()
function should be removed.