Packages — hphp pkg

HolyPHP ships with its own package manager. Packages are plain folders of .hphp files described by an hphp.json manifest, packed into a portable .hpx archive, and shared through a registry — which is itself a HolyPHP program (registry/server.hphp, ~250 lines).

Projects: hphp create

A project is a folder with an hphp.yaml. Its dependencies are installed into the project's own .hphp/packages/ folder and pinned in hphp.lock, so every project keeps its own versions and a fresh clone gets exactly the same ones with one command.

hphp create                       # asks: name, template, description, author, license, ...
hphp create shop -t web --yes     # no questions: web template, defaults

cd shop
hphp install                      # everything in hphp.yaml (keeps hphp.lock)
hphp install websocket            # add a dependency (pins "^x.y")
hphp install thread@^1.0          # ... with a version constraint
hphp install phpunit_lite --dev   # only for development (require-dev)
hphp install ../mylib             # a local package folder (path:../mylib)
hphp update                       # newest allowed versions, rewrites hphp.lock
hphp update websocket             # just one package
hphp remove websocket             # drop it (and what only it needed)
hphp deps                         # what is installed, from where
hphp run                          # runs the entry from hphp.yaml
hphp test                         # runs tests/*.hphp (a test passes when it exits 0)
hphp build                        # writes build/shop(.exe)

Templates: console (input + colored output), web (HTTP server with routes on the async library), lib (a package with tests, ready for hphp pkg publish) and empty. hphp create --help lists the options for scripts and CI (--template, --description, --author, --license, --safe, --no-git, --yes).

Layout

shop/
  hphp.yaml            name, version, entry, require, require-dev
  hphp.lock            exact versions + checksums - commit it
  src/main.hphp        the entry file
  src/routes.hphp
  tests/routes_test.hphp
  .hphp/packages/      installed dependencies - not committed (.gitignore)
  build/               hphp build output

The project file: hphp.yaml

# HolyPHP project file
name: shop
version: 0.1.0
type: project            # or: library
description: A HolyPHP web server.
entry: src/main.hphp
author: Ada
license: MIT

require:
  async: "^1.0"          # 1.x from 1.0
  websocket: "~1.2"      # 1.2.x
  mylib: "path:../mylib" # a local folder

require-dev: {}

Version constraints: "*" any, "1.2.3" exactly, "1.2" any 1.2.x, "^1.2" 1.2 up to (not including) 2.0, "~1.2" 1.2.x, ">=1.0 <2.0" a range, "^1.0 || ^2.0" either. Quote them: * and ^ mean something else in plain YAML. The file is a small YAML subset: key: value, one level of nested name: "version" lines, {} for an empty map, and # comments.

import "async"; looks in the importing file's project (.hphp/packages/async/) before the per-user package home and the libraries bundled in the compiler. Packages come from the registry; when it is unreachable or does not have them, from the libraries bundled in hphp (works offline). The registry serves the newest version of a package only, so a constraint it cannot meet fails with a clear message instead of installing something else. hphp.lock records an FNV-1a checksum of every archive; a package that changed under the same version number stops the install.

Older projects with an hphp.json keep working; when a folder has both, hphp.yaml wins. hphp pkg pack and hphp pkg publish read hphp.yaml and add a generated hphp.json to the archive, so registries and older clients understand it; .hphp/, .git/ and build/ are left out.

Official and community packages

Every package is one of three kinds; hphp deps and hphp.lock show which (trust:).

KindWhere it comes fromOn install
officialthe libraries bundled with hphpinstalled from the compiler itself, never from a registry: a registry package can not take an official name, and the registry refuses to publish one
communitya registry or a .hpx filereviewed first: you see where it is from, its author (as it claims), and what its code can do, then answer y to install
localpath:../mylibyour own code, installed without asking
$ hphp install leftpad

  ! community package: leftpad 1.2.0
    from:    registry http://hphp.ch:8930
    author:  someone   (as written in its manifest, not verified)
    files:   2 (1 HolyPHP source)
    it can:  run shell commands         leftpad.hphp:4  shell_exec("curl ... | sh");
             write or delete files      leftpad.hphp:5  unlink("/tmp/x");

    Official libraries ship with hphp and are reviewed with it. Community
    packages are written by anyone: install only what you trust. The list
    above is a hint from reading the source, not a guarantee.

  Install leftpad 1.2.0? [y/N]

The review looks for shell commands, file writes and deletes, network connections, unsafe code, native libraries (FFI), environment changes and file reads. Answering anything but y installs nothing and leaves hphp.yaml as it was. Once accepted, the package's checksum is in hphp.lock: a fresh clone runs hphp install without asking again, and a package that changed under the same version asks again. Without a terminal (CI) nothing is installed unless you pass --trust: hphp install --trust.

Quick start

hphp pkg init                     # create hphp.json skeleton in this folder
hphp pkg check                    # typecheck the package's entry file
hphp pkg pack                     # build name-version.hpx into your pkg cache
hphp pkg publish                  # pack + upload to the configured registry
hphp pkg install mathx            # download + unpack into $HPHP_HOME/packages
hphp pkg remove mathx             # uninstall (alias: uninstall)
hphp pkg list                     # show installed packages
hphp pkg search clamp             # search the registry (falls back to local)
hphp pkg registry http://localhost:8930   # point the client at a registry
hphp pkg home                     # print the package home directory

The manifest: hphp.json

{
  "name": "mathx",
  "version": "1.0.0",
  "description": "tiny math helpers",
  "entry": "mathx.hphp",
  "author": "you"
}

entry is what hphp pkg check typechecks (defaults to <name>.hphp). Packing walks the whole package folder, so every file in it ships in the archive.

Using an installed package

hphp pkg install mathx unpacks into $HPHP_HOME/packages/mathx/. The compiler's import resolution then searches:

  1. the importing file's directory (project-local files, e.g. import "mymod" → mymod.hphp)
  2. $HPHP_HOME/packages/<name>/ (installed packages)
  3. libraries embedded in the compiler (works offline)
// app.hphp — after: hphp pkg install mathx
import "mathx";

echo "clamped: " . clampi(42, 0, 10);   // 10

Run your own registry

The registry is a normal HolyPHP program using the socket builtins (stream_socket_server, stream_ready, stream_recv, stream_send). It serves a browsable index, ?q= search, package downloads under /pkg/<name>, and accepts uploads via POST /publish.

cd registry
../hphp.exe run server.hphp          # listens on http://localhost:8930
HPHP_HOME=../store ../hphp.exe run server.hphp --store /var/hphp-packages

The public community registry runs at http://hphp.ch:8930 and is the compiled-in default of the released installers — hphp pkg install thread works out of the box. You can also browse the community packages online.

Packages live as <name>.hpx + <name>.json metadata in the store directory, so a registry can be backed up, synced, or moved to another machine by copying one folder. Point clients at it from any device:

hphp pkg registry http://your-server:8930

End-to-end walkthrough

# 1. author
mkdir mathx && cd mathx && hphp pkg init
#    ... edit mathx.hphp and hphp.json ...
hphp pkg check                        # types + borrows

# 2. share (machine A)
hphp pkg publish                      # -> uploaded to the registry

# 3. consume (machine B, same registry)
hphp pkg registry http://your-server:8930
hphp pkg install mathx
hphp run app.hphp                     # import "mathx" just works

How it works under the hood