Skip to content

Commit

Permalink
Merge pull request #48 from VisualAppeal/package-update
Browse files Browse the repository at this point in the history
Updated packages
  • Loading branch information
thelfensdrfer authored Jun 4, 2020
2 parents fd0a225 + dfa04e1 commit 0d0f45f
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 249 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ language: php
dist: trusty

php:
- 7.1
- 7.2
- 7.3
- 7.4

before_script:
- composer install
- composer install --ignore-platform-reqs

script:
- travis_retry composer self-update
Expand Down
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM php:7.4-apache

MAINTAINER VisualAppeal <[email protected]>

RUN apt-get update && apt-get install -y libzip-dev libcurl4-openssl-dev
RUN docker-php-ext-install -j$(nproc) zip curl

ADD ./vendor /var/www/html/vendor
ADD ./example /var/www/html/example
ADD ./src /var/www/html/src

RUN chown -R www-data:www-data /var/www/html/example
56 changes: 6 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,22 @@ With this library your users can automatically update their instance of your app
## Installation

* Install the library via composer [visualappeal/php-auto-update](https://packagist.org/packages/visualappeal/php-auto-update)
* Create a update file/method in your application with your update routine (see `example/client/update/index.php`)
* Create an update file/method in your application with your update routine (see `example/client/update/index.php`)
* Create a `update.json` or `update.ini` on your server (where the client should get the updates, see `example/server/update.json` or `example/server/update.ini`)

**Important: Please notice that PHP needs write permissions to update the files on the webserver**

## Example

### Client
You can start an example docker container via `docker-compose up` and see the example by visiting `http://127.0.0.1:8080/example/client/`

#### update.php/some method
## Client

This file will install the update. For an example see `example/client/update/index.php`
### Caching

#### Check for new versions
The library supports the `desarrolla2/cache` component, and you should use it! Otherwise, the client will download the update ini/json file on every request.

You can always check for new versions, e.g. in the footer. This can look like this:

```php
<?php

require(__DIR__ . '/../../../vendor/autoload.php');

use \VisualAppeal\AutoUpdate;

// Download the zip update files to `__DIR__ . '/temp'`
// Copy the contents of the zip file to the current directory `__DIR__`
// The update process should last 60 seconds max
$update = new AutoUpdate(__DIR__ . '/temp', __DIR__, 60);
$update->setCurrentVersion('0.1.0'); // Current version of your application. This value should be from a database or another file which will be updated with the installation of a new version
$update->setUpdateUrl('http://php-auto-update.app/update/'); //Replace the url with your server update url

// The following two lines are optional
$update->addLogHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/update.log'));
$update->setCache(new Desarrolla2\Cache\Adapter\File(__DIR__ . '/cache'), 3600);

//Check for a new update
if ($update->checkUpdate() === false)
die('Could not check for updates! See log file for details.');

// Check if new update is available
if ($update->newVersionAvailable()) {
echo 'New Version: ' . $update->getLatestVersion();
// Simulate or install?
$simulate = true;
//Install new update
$result = $update->update($simulate);
if ($result === true) {
echo 'Update simulation successful<br>';
} else {
echo 'Update simulation failed: ' . $result . '!<br>';
}
} else {
// No new update
echo 'Your application is up to date';
}
```

The library supports the `desarrolla2/cache` component and you should use it! Otherwise the client will download the update ini/json file on every request.

### Server
## Server

Your server needs at least one file which will be downloaded from the client to check for updates. This can be a json or an ini file. See `example/server/` for examples. The ini section key respectively the json key is the version. This library uses semantic versioning to compare the versions. See [semver.org](http://semver.org/) for details. The ini/json value is the absolute url to the update zip file. Since the library supports incremental updates, the zip file only need to contain the changes since the last version. The zip files do not need to be placed on the same server, they can be uploaded to S3 or another cloud storage, too.

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"ext-json": "*",
"ext-curl": "*",
"ext-zip": "*",
"desarrolla2/cache": "2.1.*",
"monolog/monolog": "1.23.*",
"php": ">= 7.1.0",
"composer/semver": "1.4.*"
"php": "^7.3.0",
"desarrolla2/cache": "^3.0.0",
"monolog/monolog": "^2.1.0",
"composer/semver": "^3.0.0"
},
"require-dev": {
"phpunit/phpunit": "7.4.*"
"phpunit/phpunit": "^9.1.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'

services:
app:
build: .
ports:
- 8080:80
12 changes: 6 additions & 6 deletions example/client/index.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Auto Update</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="container mt-4">
<p>This is the test index.</p>

<p><a class="btn btn-primary" href="update/index.php">Update now!</a></p>
<p><a class="btn btn-primary" href="/example/client/update/index.php">Update now!</a></p>

<p>Contents of "somefile.php":</p>
<pre><?php require(__DIR__ . '/somefile.php'); ?></pre>
<p>Contents of <code>somefile.php</code>:</p>
<pre><code><?php require(__DIR__ . '/somefile.php'); ?></code></pre>
</div>
</body>
</html>
14 changes: 9 additions & 5 deletions example/client/update/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@

$update = new AutoUpdate(__DIR__ . '/temp', __DIR__ . '/../', 60);
$update->setCurrentVersion('0.1.0');
$update->setUpdateUrl('http://php-auto-update.app/server'); //Replace with your server update directory
// Replace with your server update directory
$update->setUpdateUrl('http://127.0.0.1/example/server');

// Optional:
// Log handler and cache are optional
$update->addLogHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/update.log'));
// $update->setCache(new Desarrolla2\Cache\Adapter\File(__DIR__ . '/cache'), 3600);

//Check for a new update
$cache = new Desarrolla2\Cache\File(__DIR__ . '/cache');
$update->setCache($cache, 3600);

// Check for a new update
if ($update->checkUpdate() === false) {
die('Could not check for updates! See log file for details.');
}

if ($update->newVersionAvailable()) {
//Install new update
// Install new update
echo 'New Version: ' . $update->getLatestVersion() . '<br>';
echo 'Installing Updates: <br>';
echo '<pre>';
Expand Down Expand Up @@ -57,6 +60,7 @@ function onAllUpdateFinishCallbacks($updatedVersions)
// Set the first argument (simulate) to "false" to install the update
// i.e. $update->update(false);
$result = $update->update();

if ($result === true) {
echo 'Update simulation successful<br>';
} else {
Expand Down
6 changes: 3 additions & 3 deletions example/server/update.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[0.1.0]
url = http://php-auto-update.app/server/0.1.0.zip
url = http://127.0.0.1:80/example/server/0.1.0.zip

[0.2.0]
url = http://php-auto-update.app/server/0.2.0.zip
url = http://127.0.0.1:80/example/server/0.2.0.zip

[0.2.1]
url = http://php-auto-update.app/server/0.2.1.zip
url = http://127.0.0.1:80/example/server/0.2.1.zip
6 changes: 3 additions & 3 deletions example/server/update.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"0.1.0": "http://php-auto-update.app/server/0.1.0.zip",
"0.2.0": "http://php-auto-update.app/server/0.2.0.zip",
"0.2.1": "http://php-auto-update.app/server/0.2.1.zip"
"0.1.0": "http://127.0.0.1:80/example/server/0.1.0.zip",
"0.2.0": "http://127.0.0.1:80/example/server/0.2.0.zip",
"0.2.1": "http://127.0.0.1:80/example/server/0.2.1.zip"
}
Loading

0 comments on commit 0d0f45f

Please sign in to comment.