PHPackages                             piotrpress/composer-setuper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [DevOps &amp; Deployment](/categories/devops)
4. /
5. piotrpress/composer-setuper

ActiveComposer-plugin[DevOps &amp; Deployment](/categories/devops)

piotrpress/composer-setuper
===========================

This Composer plugin allows realizing full setup process (also interactively) of your project according to defined steps.

v1.1.1(2mo ago)0712MITPHPPHP &gt;=7.4

Since Mar 28Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/PiotrPress/composer-setuper)[ Packagist](https://packagist.org/packages/piotrpress/composer-setuper)[ Docs](https://github.com/PiotrPress/composer-setuper)[ RSS](/packages/piotrpress-composer-setuper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (5)Used By (2)

Composer Setuper
================

[](#composer-setuper)

This Composer plugin allows realizing full setup process (also interactively) of your project according to defined steps.

Usage example
-------------

[](#usage-example)

Take a look at `extra.setup` section in `composer.json` file of the [WordPress Plugin Template](https://github.com/PiotrPress/wordpress-plugin-template/blob/master/composer.json) project.

Installation
------------

[](#installation)

Setuper can be simply required like a regular dependency package or can be installed like a global plugin.

### Project dependency

[](#project-dependency)

1. Add Setuper as project dependency:

```
$ composer require piotrpress/composer-setuper
```

2. Allow plugin execution:

```
$ composer config allow-plugins.piotrpress/composer-setuper true
```

### Global dependency

[](#global-dependency)

1. Add command as a global dependency:

```
$ composer global require piotrpress/composer-setuper
```

2. Allow plugin execution:

```
$ composer config -g allow-plugins.piotrpress/composer-setuper true
```

Defining setup
--------------

[](#defining-setup)

### Actions

[](#actions)

Actions are setup steps that are firing during the Composer execution process.

You can define actions in `composer.json` [extra](https://getcomposer.org/doc/04-schema.md#extra) property as an array of objects in `setup` property e.g.:

```
{
  "extra": {
    "setup": [
      {
        "action": "write",
        "message": "Hello world!"
      }
    ]
  }
}
```

Setup can also be a PHP callback (defined as a static method) e.g.:

```
{
  "extra": {
    "setup": "MyVendor\\MyClass::setup"
  }
}
```

```
namespace MyVendor;

class MyClass {
    static public function setup() {
        return [
            [
                'action' => 'write',
                'message' => 'Hello world!'
            ]
        ];
    }
}
```

Single action can also be a PHP callback (defined as a static method) e.g.:

```
{
  "extra": {
    "setup": [
      {
        "action": "write",
        "message": "Hello"
      },
      "MyVendor\\MyClass::writeWorld"
    ]
  }
}
```

```
namespace MyVendor;

class MyClass {
    static public function writeWorld() {
        return [
            'action' => 'write',
            'message' => 'world!'
        ];
    }
}
```

**NOTE:** PHP classes containing defined callbacks must be autoloadable via Composer's autoload functionality.

### Events

[](#events)

Actions are triggered by [events](https://getcomposer.org/doc/articles/scripts.md#event-names).

```
{
  "extra": {
    "setup": [
      {
        "action": "write",
        "message": "Hello world!",
        "event": "post-update-cmd"
      }
    ]
  }
}
```

If you don't specify the event for the action, then it'll be assigned to `setup` script, which you can use in `composer.json` [scripts](https://getcomposer.org/doc/articles/scripts.md#defining-scripts) property e.g.:

```
{
  "scripts": {
    "post-update-cmd": "@setup"
  }
}
```

You can also run it manually from command line:

```
$ composer run setup
```

### Priorities

[](#priorities)

Actions calls within an event are sorted by [priority](https://getcomposer.org/doc/articles/plugins.md#event-handler). By default, the priority of an action is set to `0`. Higher value represent higher priorities.

```
{
  "extra": {
    "setup": [
      {
        "action": "write",
        "message": "world!"
      },
      {
        "action": "write",
        "message": "Hello",
        "priority": 1
      }
    ]
  }
}
```

Input / Output actions
----------------------

[](#input--output-actions)

The most powerfull and common usage feature of Setuper is interactively communicating with the user due setup process. Setuper can taking users inserted values using `insert`, `secret`, `select` or `confirm` actions and sets the variables for future reference in other (e.g. filesystem manipulation) actions using `{$variable}` syntax.

```
[
    {
      "action": "set",
      "variable": "greeting",
      "value": "Hello world!"
    },
    {
      "action": "write",
      "message": "{$greeting} from Setuper!"
    }
]
```

### Set

[](#set)

Sets value to variable for future reference.

```
{
  "action": "set",
  "variable": "greeting",
  "value": "Hello world!"
}
```

and/or:

```
{
  "action": "set",
  "file": ".env"
}
```

#### Parameters

[](#parameters)

Required:

- `variable` - `string`|`string[]` - name of the variable following the PHP naming [rules](https://www.php.net/manual/en/language.variables.basics.php).
- `value` - `mixed`|`mixed[]` - value of the variable.

and/or:

- `file` - `string`|`string[]` - path to the file containing variables.

**NOTE:** `file` parameter uses [parse\_ini\_file()](https://www.php.net/manual/en/function.parse-ini-file.php) function, so for detailed info (e.g. how file format looks like and how to use `environment variables`) check out the documentation. Worth mentioning that `.env` file format is supported as well.

### Insert

[](#insert)

Sets value to variable passed by the user.

```
{
  "action": "insert",
  "variable": "greeting",
  "message": "Insert your greeting:"
}
```

#### Parameters

[](#parameters-1)

- `variable` - `required`|`string` - name of the variable following the PHP naming [rules](https://www.php.net/manual/en/language.variables.basics.php).
- `message` - `required`|`string` - outputs message for the user to the console.
- `required` - `optional`|`boolean` - whether the value is required or not.
- `validator` - `optional`|`collable`|`collable[]` - the PHP callback (defined as a static method)
- `default` - `optional`|`string` - the default value if none is given by the user.

**NOTE:** `insert` action uses [ask()](https://symfony.com/doc/current/components/console/helpers/questionhelper.html#validating-the-answer) function from Symfony Console component, so for detailed info (e.g. how validating the value) check out the documentation.

### Secret

[](#secret)

Sets value to variable passed by the user.

```
{
  "action": "secret",
  "variable": "password",
  "message": "Insert your password:"
}
```

#### Parameters

[](#parameters-2)

- `variable` - `required`|`string` - name of the variable following the PHP naming [rules](https://www.php.net/manual/en/language.variables.basics.php).
- `message` - `required`|`string` - outputs message for the user to the console.

### Select

[](#select)

Sets value to variable passed by the user.

```
{
  "action": "select",
  "variable": "color",
  "message": "Select your favourite color:",
  "choices": [ "red", "green", "blue" ]
}
```

#### Parameters

[](#parameters-3)

- `variable` - `required`|`string` - name of the variable following the PHP naming [rules](https://www.php.net/manual/en/language.variables.basics.php).
- `message` - `required`|`string` - outputs message for the user to the console.
- `choices` - `required`|`string[]` - an array of choices.
- `default` - `optional`|`string` - the default value if none is given by the user.
- `error` - `optional`|`string` - the error message if the value is not in the choices array.
- `multiple` - `optional`|`boolean` - whether the user can select multiple choices or not.

### Confirm

[](#confirm)

Sets value to variable passed by the user.

```
{
  "action": "confirm",
  "variable": "help",
  "message": "Can I help you?"
}
```

#### Parameters

[](#parameters-4)

- `variable` - `required`|`string` - name of the variable following the PHP naming [rules](https://www.php.net/manual/en/language.variables.basics.php).
- `message` - `required`|`string` - outputs message for the user to the console.
- `default` - `optional`|`boolean` - the default value if none is given by the user.

### Write

[](#write)

Outputs single or multiple lines message to the console.

```
{
  "action": "write",
  "message": "Hello world!"
}
```

#### Parameters

[](#parameters-5)

- `message` - `required`|`string`|`string[]` - the message as a single string or an array of lines.
- `verbose` - `optional`|`string` - one of the verbosity level: `quiet`, `normal`, `verbose`, `very_verbose`, `debug`

**NOTE:** `write` action uses [writeln()](https://symfony.com/doc/current/console/coloring.html) function from Symfony Console component, so for detailed info (e.g. how to color and style the message) check out the documentation.

Command action
--------------

[](#command-action)

Executes a command.

```
{
  "action": "command",
  "command": "ls -la"
}
```

#### Parameters

[](#parameters-6)

- `command` - `required`|`string`|`string[]` - the command to execute. An array may be used to designate multiple commands.
- `verbose` - `optional`|`string` - one of the verbosity level: `quiet`, `normal`, `verbose`, `very_verbose`, `debug`

Filesystem actions
------------------

[](#filesystem-actions)

### Directory

[](#directory)

Creates a directory.

```
{
  "action": "directory",
  "path": "src"
}
```

#### Parameters

[](#parameters-7)

- `path` - `required`|`string`|`string[]` - path to the directory. An array may be used to designate multiple directories.

### Symlink

[](#symlink)

Creates a symbolic link.

```
{
  "action": "symlink",
  "source": "src",
  "target": "inc"
}
```

#### Parameters

[](#parameters-8)

- `source` - `required`|`string`|`string[]` - path to the source directory. An array may be used to designate multiple directories.
- `target` - `required`|`string`|`string[]` - path to the target directory. An array may be used to designate multiple directories.

### Rename

[](#rename)

Renames a file or directory.

```
{
  "action": "rename",
  "source": "src/hello.php",
  "target": "src/hi.php"
}
```

#### Parameters

[](#parameters-9)

- `source` - `required`|`string`|`string[]` - path to the source file or directory. An array may be used to designate multiple files or directories.
- `target` - `required`|`string`|`string[]` - path to the target file or directory. An array may be used to designate multiple files or directories.

### Copy

[](#copy)

Copies a file or directory.

```
{
  "action": "copy",
  "source": "src/hello.php",
  "target": "inc/hello.php"
}
```

#### Parameters

[](#parameters-10)

- `source` - `required`|`string`|`string[]` - path to the source file or directory. An array may be used to designate multiple files or directories.
- `target` - `required`|`string`|`string[]` - path to the target file or directory. An array may be used to designate multiple files or directories.

### Move

[](#move)

Moves a file or directory.

```
{
  "action": "move",
  "source": "src/hello.php",
  "target": "inc/hello.php"
}
```

#### Parameters

[](#parameters-11)

- `source` - `required`|`string`|`string[]` - path to the source file or directory. An array may be used to designate multiple files or directories.
- `target` - `required`|`string`|`string[]` - path to the target file or directory. An array may be used to designate multiple files or directories.

### Remove

[](#remove)

Removes a file or directory.

```
{
  "action": "remove",
  "path": "src/hello.php"
}
```

#### Parameters

[](#parameters-12)

- `path` - `required`|`string`|`string[]` - path to the file or directory. An array may be used to designate multiple files or directories.

### Owner

[](#owner)

Changes the owner of a file or directory.

```
{
  "action": "owner",
  "path": "src/hello.php",
  "owner": "www-data"
}
```

#### Parameters

[](#parameters-13)

- `path` - `required`|`string`|`string[]` - path to the file or directory. An array may be used to designate multiple files or directories.
- `owner` - `required`|`string`|`string[]` - the owner value. An array may be used to designate multiple owners.

### Group

[](#group)

Changes the group of a file or directory.

```
{
  "action": "group",
  "path": "src/hello.php",
  "group": "www-data"
}
```

#### Parameters

[](#parameters-14)

- `path` - `required`|`string`|`string[]` - path to the file or directory. An array may be used to designate multiple files or directories.
- `group` - `required`|`string`|`string[]` - the group value. An array may be used to designate multiple groups.

### Mode

[](#mode)

Changes the mode of a file or directory.

```
{
  "action": "mode",
  "path": "src/hello.php",
  "mode": "0755"
}
```

#### Parameters

[](#parameters-15)

- `path` - `required`|`string`|`string[]` - path to the file or directory. An array may be used to designate multiple files or directories.
- `mode` - `required`|`string`|`string[]` - the mode value. An array may be used to designate multiple modes.

File content actions
--------------------

[](#file-content-actions)

### Dump

[](#dump)

Dump the content to the file.

```
{
  "action": "dump",
  "content": "
