Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
HookContentRenderer
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 render
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\GitHooks;
21
22use FastForward\DevTools\Path\DevToolsPathResolver;
23
24/**
25 * Renders packaged Git hooks with runtime-specific DevTools hook configuration paths.
26 */
27 class HookContentRenderer
28{
29    /**
30     * Placeholder replaced with the packaged GrumPHP config path rendered relative to the project when possible.
31     */
32    public const string MANAGED_GRUMPHP_CONFIG_PLACEHOLDER = '__DEV_TOOLS_GRUMPHP_CONFIG__';
33
34    /**
35     * Renders the hook contents for the active DevTools runtime.
36     *
37     * @param string $contents the packaged hook contents
38     * @param string $projectPath the consumer project root that will own the synchronized hook
39     *
40     * @return string the rendered hook contents
41     */
42    public function render(string $contents, string $projectPath = ''): string
43    {
44        return str_replace(
45            self::MANAGED_GRUMPHP_CONFIG_PLACEHOLDER,
46            escapeshellarg(DevToolsPathResolver::getPackagePathRelativeToProject('grumphp.yml', $projectPath)),
47            $contents,
48        );
49    }
50}