<?php

namespace Deployer;

require_once 'recipe/common.php';
require_once 'deploy/git.php';

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);

// Shared files/dirs between deploys
set('shared_dirs', ['var/log', 'var/sessions']);
set('shared_files', ['.env.local']);

//set('writable_dirs', ['.']);
//set('writable_mode', 'sticky');
//set('http-group', 'www-data');

set('migrations_config', '');
set('allow_anonymous_stats', false);

set('console_options', fn() => '--no-interaction');
set('bin/console', fn() => parse('{{release_path}}/bin/console'));

set('composer_options', '--verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader --no-scripts');

desc('Clear cache');
task('cache:clear', fn() => run('{{bin/php}} {{bin/console}} cache:clear {{console_options}} --no-warmup'));

desc('Warm up cache');
task('cache:warmup', fn() => run('{{bin/php}} {{bin/console}} cache:warmup {{console_options}}'));

desc('Migrate database');
task('database:migrate', function () {
    $options = '--allow-no-migration';
    if (get('migrations_config') !== '') {
        $options = sprintf('%s --configuration={{release_path}}/{{migrations_config}}', $options);
    }

    run(sprintf('{{bin/php}} {{bin/console}} doctrine:migrations:migrate %s {{console_options}}', $options));
});

desc('Shows current deployed version');
task('deploy:current', function () {
    $current = run('readlink {{deploy_path}}/current');
    writeln("Current deployed version: $current");
});

desc('Deploy project');
task('deploy', [
    'deploy:prepare',
    'deploy:vendors',
    'database:migrate',
    'cache:clear',
    'cache:warmup',
    'deployment:log',
    'deploy:symlink',
    'deploy:unlock',
    'deploy:cleanup',
    'deploy:current',
]);

after('deploy', 'deploy:success');

after('deploy:failed', 'deploy:unlock');
