LGV_TZ_Lookup 1.4.2
Time zone lookup for PHP applications and HTTP clients
Loading...
Searching...
No Matches
demo.php File Reference

This is the PHP worker for the command-line Composer demo. More...

Go to the source code of this file.

Functions

 demoCleanup (string $directory)
 This removes the downloads and drops the database owned by this demo run.
 demoConnection (?string $database=null, ?string $driver=null)
 This opens an administrative or test-database connection for the selected backend. Connection settings come from DRIVER, HOST, PORT, USER, PASSWORD, and ADMIN_DATABASE. PostgreSQL administration connects to the existing "postgres" database by default; loaded data always goes into a separate temporary database.
 demoCreateDatabase (PDO $admin, string $directory)
 This creates a uniquely named database and records it for cleanup.
 demoDownload (string $url, string $destination)
 This downloads one HTTPS resource directly to a file.
 demoDriver ()
 This validates the selected database driver.
 demoEnvironment (string $name, string $default)
 This reads a demo setting from the environment.
 demoExtract (string $archive, string $destination)
 This extracts the archive's single GeoJSON file into a controlled local filename.
 demoInstall (string $directory)
 This builds the temporary Composer application and loads its autoloader.
 demoLatency (array $values)
 This formats the timing statistics for one group of lookup calls.
 demoPrintMetrics ()
 This prints the collected durations, data sizes, memory measurements, and test coverage.
 demoRemoveDownloads (string $directory)
 This removes the compressed boundary archive and the extracted GeoJSON.
 demoResetMemoryPeak ()
 This starts a new peak-memory measurement, when the PHP version supports it.
 demoRun (string $directory)
 This performs the complete Composer installation, data load, system test, and metrics run.
 demoTiming (string $label, $start)
 This records the elapsed time for one completed phase.

Variables

 $demoMetrics
 These are the measurements collected by the worker.
const DEMO_PACKAGE
 This is the package installed into the temporary Composer application.
const DEMO_RELEASE_API
 This GitHub endpoint identifies the latest published Timezone Boundary Builder release.
if(realpath($_SERVER['SCRIPT_FILENAME'] ?? '') !==__FILE__) if(PHP_SAPI !=='cli'||$argc !==3||!in_array( $argv[1],[ 'run', 'cleanup'], true)||!is_dir( $argv[2])) if(function_exists('pcntl_async_signals')) try
 This is the worker entrypoint.

Detailed Description

© Copyright 2023-2026, Little Green Viper Software Development LLC

LICENSE:

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Little Green Viper Software Development LLC

Call demo/run.sh to run the demo. The shell creates a temporary working directory, and calls this file in either "run" or "cleanup" mode. In "run" mode, we install the released package through Composer, load the latest boundary data, check the resulting database, run location tests, and display metrics. The lookup classes come from the temporary Composer application, so this is a test of the installed package.

The separate "cleanup" invocation lets the shell remove the database and downloads, even if the loader's PHP process runs out of memory. The shell removes the remaining working directory after database cleanup succeeds.

Definition in file demo.php.

Function Documentation

◆ demoCleanup()

demoCleanup ( string $directory)

The database name comes from database.json, and must match the generated demo naming pattern. We delete large files before connecting to the database server, so they are removed even when the server is unavailable. If dropping the database fails, its cleanup record remains for recovery. The shell removes the working directory only on success.

Returns
: True, if cleanup succeeded, or no database had been created.
Exceptions
RuntimeExceptionif a cleanup record is unexpected, or a file cannot be removed.
PDOExceptionif database cleanup fails.
Parameters
$directoryThe working directory owned by this demo run.

Definition at line 285 of file demo.php.

References $directory, $start, demoCleanup(), and demoRemoveDownloads().

Referenced by demoCleanup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoConnection()

demoConnection ( ?string $database = null,
?string $driver = null )
Returns
: A PDO connection with exception reporting enabled.
Exceptions
RuntimeExceptionif the configured port is invalid.
PDOExceptionif the connection fails.
Parameters
$databaseTest database, or NULL for the administrative database.
$driverRecorded cleanup driver, or NULL for the environment setting.

Definition at line 191 of file demo.php.

References $database, $driver, $port, demoConnection(), demoDriver(), and demoEnvironment().

Referenced by demoConnection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoCreateDatabase()

demoCreateDatabase ( PDO $admin,
string $directory )

CREATE does not use IF NOT EXISTS, so a name collision cannot adopt an existing database. Where pcntl is available, interruption signals are blocked until the cleanup record has been written. If the record cannot be written, we drop the new database before reporting the failure.

Returns
: The new database name, beginning with "lgv_tz_demo_".
Exceptions
RuntimeExceptionif the cleanup record cannot be written.
PDOExceptionif database creation or rollback fails.
Parameters
$adminAn administrative connection with CREATE and DROP permissions.
$directoryThe working directory in which database.json is saved.

Definition at line 326 of file demo.php.

References $admin, $directory, $driver, $name, $quote, and demoCreateDatabase().

Referenced by demoCreateDatabase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoDownload()

demoDownload ( string $url,
string $destination )

Redirects must also use HTTPS. We stream the response to disk, so the large boundary archive is never read into a single PHP string. A partial download remains in the working directory for the shell's cleanup to remove.

Exceptions
RuntimeExceptionif the destination cannot be opened, or the transfer fails.
Parameters
$urlThe HTTPS resource to download.
$destinationThe local filename to create in the demo's working directory.

Definition at line 220 of file demo.php.

References demoDownload().

Referenced by demoDownload().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoDriver()

demoDriver ( )
Returns
: "mysql" (the default) or "pgsql".
Exceptions
RuntimeExceptionif another driver is requested.

Definition at line 174 of file demo.php.

References $driver, demoDriver(), and demoEnvironment().

Referenced by demoConnection(), demoDriver(), and demoInstall().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoEnvironment()

demoEnvironment ( string $name,
string $default )

The "LGV_TZ_DEMO_" prefix is added to the provided name. An explicitly set empty value is retained, which is useful for a MySQL user with an empty password.

Returns
: The environment value, or the supplied default when the variable is not set.
Parameters
$nameThe setting name, without the LGV_TZ_DEMO_ prefix.
$defaultThe value to use when the setting is absent.

Definition at line 161 of file demo.php.

References $name, and demoEnvironment().

Referenced by demoConnection(), demoDriver(), demoEnvironment(), and demoInstall().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoExtract()

demoExtract ( string $archive,
string $destination )

The ZIP may contain other files, but must contain exactly one .json or .geojson entry. We stream that entry into our own destination, instead of using paths from the archive. The extracted size must match the ZIP entry's size.

Exceptions
RuntimeExceptionif the archive cannot be read, its contents are unexpected, or extraction fails.
Parameters
$archiveThe downloaded boundary ZIP file.
$destinationThe local GeoJSON filename to create.

Definition at line 235 of file demo.php.

References demoExtract().

Referenced by demoExtract().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoInstall()

demoInstall ( string $directory)

We download and verify Composer, write the application's composer.json, and install the published lookup package and its optional JSON parser. Composer's home and cache are placed in the working directory, as well. Installation does not execute package scripts or plugins, and does not change the user's installed Composer.

Exceptions
RuntimeExceptionif checksum verification or Composer installation fails.
Parameters
$directoryThe demo's temporary application directory.

Definition at line 251 of file demo.php.

References $directory, demoDriver(), demoEnvironment(), and demoInstall().

Referenced by demoInstall().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoLatency()

demoLatency ( array $values)

We report the arithmetic mean, the median, the nearest-rank 95th percentile, and the slowest call.

Returns
: A printable string. An empty group reports that no lookup measurements are available.
Parameters
$valuesIndividual lookup durations, in milliseconds.

Definition at line 91 of file demo.php.

References demoLatency().

Referenced by demoLatency().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoPrintMetrics()

demoPrintMetrics ( )

Lookup timings cover calls to the package API, using an existing connection. They exclude downloading, loading, and reference-geometry calculations. PHP memory measurements describe this worker, and do not include the database server or the separate Composer process. Large-polygon audit counters show which geometry was actually evaluated.

Definition at line 109 of file demo.php.

References $demoMetrics, $seconds, and demoPrintMetrics().

Referenced by demoPrintMetrics().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoRemoveDownloads()

demoRemoveDownloads ( string $directory)

We call this as soon as loading finishes, and again during cleanup. Already-removed files are harmless.

Exceptions
RuntimeExceptionif a remaining boundary file cannot be removed.
Parameters
$directoryThe working directory containing the downloaded files.

Definition at line 264 of file demo.php.

References $directory, and demoRemoveDownloads().

Referenced by demoCleanup(), and demoRemoveDownloads().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoResetMemoryPeak()

demoResetMemoryPeak ( )

PHP 8.2 and later can reset the peak between phases and lookup calls. Earlier versions leave the lifetime peak intact; the report labels those measurements accordingly.

Definition at line 79 of file demo.php.

References demoResetMemoryPeak(), and if.

Referenced by demoResetMemoryPeak().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoRun()

demoRun ( string $directory)

We first check PHP and the database server, then install the package and obtain the latest boundary release. The package performs the actual load while a listener retains independent source geometry for storage and lookup checks. The large files are deleted after loading, and the reference geometry is freed before lookup measurements.

The known locations come from the installed package. Generated tests sample the largest polygons, including points on both sides of boundaries. Coverage must show that those polygons were actually decoded by the API. The parent shell handles database and directory cleanup after this function returns or the worker fails.

Returns
: 0, if every check passes; 1, if a storage, lookup, or coverage check fails.
Exceptions
RuntimeExceptionif setup, download, loading, or test preparation cannot complete.
Parameters
$directoryThe private working directory created by run.sh.

Definition at line 363 of file demo.php.

References $demoMetrics, $directory, and demoRun().

Referenced by demoRun().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demoTiming()

demoTiming ( string $label,
$start )
Parameters
$labelThe label displayed in the metrics report.
$startThe phase's starting hrtime(true) value, in nanoseconds.

Definition at line 65 of file demo.php.

References $demoMetrics, $start, and demoTiming().

Referenced by demoTiming().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ $demoMetrics

$demoMetrics

"started" contains a monotonic timestamp in nanoseconds. "timings" contains phase durations in seconds. The lookup arrays contain individual call durations in milliseconds. Fields ending in "bytes" describe data sizes or PHP memory usage. "largest" contains only the small description records needed for the final report.

We retain measurements from completed phases, so a failed run can still print the information available so far.

Definition at line 58 of file demo.php.

Referenced by demoPrintMetrics(), demoRun(), and demoTiming().

◆ DEMO_PACKAGE

const DEMO_PACKAGE

Definition at line 43 of file demo.php.

◆ DEMO_RELEASE_API

const DEMO_RELEASE_API

Definition at line 46 of file demo.php.

◆ try

if(realpath( $_SERVER[ 'SCRIPT_FILENAME'] ?? '') !==__FILE__) if(PHP_SAPI !=='cli'|| $argc !==3||!in_array($argv[1], ['run', 'cleanup'], true)||!is_dir($argv[2])) if (function_exists( 'pcntl_async_signals')) try

run.sh supplies the mode and working directory. An invalid invocation exits with 2. A caught worker error exits with 1, after printing any collected metrics. The shell preserves the worker's exit status unless cleanup fails.

Definition at line 575 of file demo.php.

Referenced by updaterFailure().