Cherrycake
ExamplesGithub
version 2.x alpha
version 2.x alpha
  • Introduction
  • Status
  • Changelog
  • Migration
  • Architecture
    • Basics
    • Modules
    • Classes
    • Lifecycle
      • Deep lifecycle
    • Performance
    • Security
    • Patterns
      • Files structure
    • Items
    • Server requirements
  • Guide
    • Getting started
      • Skeleton start
      • Docker start
    • Modules guide
    • Classes guide
    • Actions guide
      • Complex actions
      • Variable path components
      • Accept GET or POST parameters
      • Getting the URL of an action
      • Cached actions
      • Brute force attacks
    • Patterns guide
      • Passing variables to a pattern
      • Nested patterns
      • Cached patterns
    • Cache guide
      • Time To Live
      • Using cache
      • Lists
      • Queues
      • Pools
    • Database guide
      • Basic queries
      • Prepared queries
      • Cached queries
      • Cache key naming
      • Removing queries from cache
    • Items guide
      • Item cache
      • Item lists
      • Items custom filters
      • Items custom ordering
      • Mixing filters and ordering
      • Items with relationships
      • Items cache
    • HtmlDocument guide
    • Css and Javascript guide
      • Modules injecting CSS and JavaScript
    • Session guide
    • Login guide
      • Creating a complete login workflow
    • Locale guide
      • Multilingual texts
      • Domain based site localization
    • Log guide
      • Loading Log events from the database
    • Stats guide
      • Stats events with additional dimensions
      • Loading Stats events from the database
    • Janitor guide
      • Janitor tasks configuration files
    • Command line interface
    • Debugging
  • Reference
    • Core modules
      • Actions
        • Actions methods
      • Browser
      • Cache
        • Cache methods
      • Css
        • Css methods
      • Database
      • Email
      • Errors
      • HtmlDocument
        • HtmlDocument methods
      • ItemAdmin
      • Janitor
        • Janitor methods
      • Javascript
        • Javascript methods
      • Locale
        • Locale methods
      • Log
        • Log methods
      • Login
        • Login methods
      • Output
        • Output methods
      • Patterns
        • Patterns methods
      • Security
        • Security methods
      • Session
        • Session methods
      • Stats
        • Stats methods
      • SystemLog
      • TableAdmin
      • Translation
      • Validate
    • Core classes
      • Action
        • Action methods
        • Action properties
      • AjaxResponseJson
      • BasicObject
        • BasicObject methods
      • CacheProvider
        • CacheProvider methods
      • Color
      • DatabaseProvider
        • DatabaseProvider methods
      • DatabaseResult
        • DatabaseResult methods
        • DatabaseResult properties
      • DatabaseRow
      • Engine
        • Engine methods
        • Engine properties
      • Gradient
      • Item
        • Item methods
        • Item properties
      • Items
        • Items methods
        • Items properties
      • Image
      • JanitorTask
        • JanitorTask methods
        • JanitorTask properties
      • LogEvent
        • LogEvent methods
        • LogEvent Properties
      • LogEvents
        • LogEvents methods
      • Module
        • Module methods
        • Module properties
      • Response
      • Request
        • Request methods
      • RequestParameter
        • RequestParameter methods
      • RequestPathComponent
        • RequestPathComponent methods
      • Result
      • StatsEvent
        • StatsEvent properties
      • StatsEvents
        • StatsEvents methods
      • SystemLogEvent
        • SystemLogEvent methods
        • SystemLogEvent properties
      • SystemLogEvents
        • SystemLogEvents methods
  • Code conventions
  • License
  • Extras
Powered by GitBook
On this page
  • Executing CLI Actions
  • Apps that both attend web requests and CLI actions
  • Running an app from the command line
  • Passing parameters to CLI actions

Was this helpful?

  1. Guide

Command line interface

PreviousJanitor tasks configuration filesNextDebugging

Last updated 3 years ago

Was this helpful?

Cherrycake apps can run as command line applications that are invoked from an operating system prompt like the Linux shell.

To let your app attend requests from the command line, you set up an just like any other, except this time you use the class when mapping it, like this:

$e->Actions->mapAction(
    "helloWorldCli",
    new \Cherrycake\Actions\ActionCli([
        "moduleType" => \Cherrycake\ACTION_MODULE_TYPE_APP,
        "moduleName" => "HelloWorld",
        "methodName" => "sayHi"
    ])
);

And in your method, you use the class instead of the usual or :

function sayHi() {
    global $e;
    $e->Output->setResponse(new \Cherrycake\Actions\ResponseCli([
        "payload" => "Hello World from the Cli interface"
    ]));
}

Executing CLI Actions

If you remember how we created the index.php file in the guide, you'll remember that the method we called to make Cherrycake starting working on the received request actions was . When creating an app that works in the command line, the method to use is instead, like this:

<?php

namespace CherrycakeApp;

require "vendor/autoload.php";

$e = new \Cherrycake\Engine;

if ($e->init(__NAMESPACE__, [
    "isDevel" => true
]))
    $e->attendCliRequest();

$e->end();

Apps that both attend web requests and CLI actions

Sometimes you'll want your app to attend web requests like a normal web application, but also attend some CLI actions that you'll use to perform maintenance work, run batch processes or similar tasks that are triggered by an admin from the command line, and not by a client using a browser.

Running an app from the command line

php -f ./cli.php helloWorldCli
Hello World from the Cli interface

Passing parameters to CLI actions

$e->Actions->mapAction(
    "userFlushCache",
    new \Cherrycake\Actions\ActionCli([
        "moduleType" => \Cherrycake\ACTION_MODULE_TYPE_APP,
        "moduleName" => "Users",
        "methodName" => "flushUserCacheCli",
        "parameters" => [
            new \Cherrycake\Actions\RequestParameter([
                "type" => \Cherrycake\REQUEST_PARAMETER_TYPE_CLI,
                "name" => "userId",
                "securityRules" => [
                    \Cherrycake\SECURITY_RULE_TYPICAL_ID
                ]
            ])
        ]
    ])
);

And you receive the parameters just like you do with GET or POST:

function flushUserCacheCli($request) {
    global $e;
    $user = new User([
        "loadMethod" => "fromId",
        "id" => $request->id
    ]);
    $user->clearCache();
    $e->Output->setResponse(new \Cherrycake\Actions\ResponseCli([
        "payload" => "Cache for user ".$request->userId." flushed"
    ]));
}

Now, to call a CLI action that accepts parameters from the command line, you use the regular UNIX parameters syntax after the action name, like this:

php -f ./cli.php userFlushCache --userId=832
Cache for user 832 flushed

A common solution is to create a cli.php file additionally to the index.php file. This cli.php will look more or less equal to your existing index.php, but it will call the instead of method.

The provides a cli.php file where you'll see this solution at work.

To run a Cherrycake app from the command line in Linux, you use the executable to run the cli.php file (or whatever name you choose for your main .php file), and pass the name as the first parameter.

Following our example above, to run helloWorldCli , we would call Cherrycake from the Linux command line like this:

Just like regular Actions can receive GET and POST parameters, CLI actions can receive command line parameters. To map an that receives parameters, you pass the parameters array when creating the object just like you already did in the of the , except this time you use the parameter type instead of REQUEST_PARAMETER_TYPE_GET or REQUEST_PARAMETER_TYPE_POST, like this:

Cherrycake Skeleton repository
PHP cli
Action
Action
Action
Request
Accept GET or POST parameters
Actions Guide
ResponseCli
ResponseTextHtml
ResponseTextPlain
Engine:attendCliRequest
Engine:attendWebRequest
Engine:attendWebRequest
Engine:attendCliRequest
Getting started
REQUEST_PARAMETER_TYPE_CLI
ActionCli
ActionCli