Skip to main content

PHPWorker

A PHP client that can be used to run PHP code in the browser.

Implements

  • LimitedPHPApi

Index

Constructors

constructor

Properties

absoluteUrl

absoluteUrl: string = ''
@inheritDoc

documentRoot

documentRoot: string = ''
@inheritDoc

Methods

public__internal_setRequestHandler

addEventListener

  • addEventListener(eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy, listener: PHPEventListener): void
  • @inheritDoc

    Parameters

    • eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
    • listener: PHPEventListener

    Returns void

chdir

  • chdir(path: string): void
  • @inheritDoc

    Parameters

    • path: string

    Returns void

defineConstant

  • defineConstant(key: string, value: null | string | number | boolean): void
  • @inheritDoc

    Parameters

    • key: string
    • value: null | string | number | boolean

    Returns void

fileExists

  • fileExists(path: string): boolean
  • @inheritDoc

    Parameters

    • path: string

    Returns boolean

internalUrlToPath

  • internalUrlToPath(internalUrl: string): string
  • Converts an absolute URL based at the PHPRequestHandler to a relative path without the server pathname and scope.


    Parameters

    • internalUrl: string

      An absolute URL based at the PHPRequestHandler root.

    Returns string

    The relative path.

isDir

  • isDir(path: string): boolean
  • @inheritDoc

    Parameters

    • path: string

    Returns boolean

isFile

  • isFile(path: string): boolean
  • @inheritDoc

    Parameters

    • path: string

    Returns boolean

listFiles

  • @inheritDoc

    Parameters

    Returns string[]

mkdir

  • mkdir(path: string): void
  • @inheritDoc

    Parameters

    • path: string

    Returns void

mkdirTree

  • mkdirTree(path: string): void
  • @inheritDoc

    Parameters

    • path: string

    Returns void

mv

  • mv(fromPath: string, toPath: string): Promise<void>
  • Moves a file or directory in the PHP filesystem to a new location.


    Parameters

    • fromPath: string
    • toPath: string

    Returns Promise<void>

onDownloadProgress

  • onDownloadProgress(callback: (progress: CustomEvent<ProgressEvent<EventTarget>>) => void): Promise<void>
  • The onDownloadProgress event listener.


    Parameters

    • callback: (progress: CustomEvent<ProgressEvent<EventTarget>>) => void

    Returns Promise<void>

onMessage

  • @inheritDoc

    Parameters

    Returns () => Promise<void>

      • (): Promise<void>
      • Returns Promise<void>

pathToInternalUrl

  • pathToInternalUrl(path: string): string
  • Converts a path to an absolute URL based at the PHPRequestHandler root.


    Parameters

    • path: string

      The server path to convert to an absolute URL.

    Returns string

    The absolute URL.

readFileAsBuffer

  • readFileAsBuffer(path: string): Uint8Array
  • @inheritDoc

    Parameters

    • path: string

    Returns Uint8Array

readFileAsText

  • readFileAsText(path: string): string
  • @inheritDoc

    Parameters

    • path: string

    Returns string

removeEventListener

  • removeEventListener(eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy, listener: PHPEventListener): void
  • @inheritDoc

    Parameters

    • eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
    • listener: PHPEventListener

    Returns void

request

  • Serves the request – either by serving a static file, or by dispatching it to the PHP runtime.

    The request() method mode behaves like a web server and only works if the PHP was initialized with a requestHandler option (which the online version of WordPress Playground does by default).

    In the request mode, you pass an object containing the request information (method, headers, body, etc.) and the path to the PHP file to run:

    const php = PHP.load('7.4', {
    requestHandler: {
    documentRoot: "/www"
    }
    })
    php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
    const result = await php.request({
    method: "GET",
    headers: {
    "Content-Type": "text/plain"
    },
    body: "Hello world!",
    path: "/www/index.php"
    });
    // result.text === "Hello world!"

    The request() method cannot be used in conjunction with cli().


    Parameters

    Returns Promise<PHPResponse>

rmdir

  • Removes a directory from the PHP filesystem.


    Parameters

    • path: string

      The directory path to remove.

    • optionaloptions: RmDirOptions

      Options for the removal.

    Returns Promise<void>

run

setPrimaryPHP

  • setPrimaryPHP(php: PHP): Promise<void>
  • Parameters

    Returns Promise<void>

setSapiName

  • setSapiName(newName: string): void
  • @inheritDoc

    Parameters

    • newName: string

    Returns void

unlink

  • unlink(path: string): void
  • @inheritDoc

    Parameters

    • path: string

    Returns void

writeFile

  • writeFile(path: string, data: string | Uint8Array): void
  • @inheritDoc

    Parameters

    • path: string
    • data: string | Uint8Array

    Returns void