• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / aplus / 12592612875

31 Jul 2023 08:46PM UTC coverage: 61.386% (+1.2%) from 60.227%
12592612875

push

github

natanfelles
Merge branch 'migration' into development

62 of 101 relevant lines covered (61.39%)

1.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.06
/src/Commands/NewCommand.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Command Line Tool.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Aplus\Commands;
11

12
use Framework\CLI\CLI;
13
use Framework\CLI\Command;
14
use RecursiveDirectoryIterator;
15
use RecursiveIteratorIterator;
16

17
/**
18
 * Class NewCommand.
19
 *
20
 * @package aplus
21
 */
22
abstract class NewCommand extends Command
23
{
24
    protected function create(string $package, string $name) : void
25
    {
26
        $directory = $this->getDirectory();
2✔
27
        $source = $this->getComposerSource($package);
2✔
28
        if ( ! $source) {
2✔
29
            $source = $this->getComposerSource($package, true);
×
30
            if ( ! $source) {
×
31
                $source = $this->getDistroSource($package);
×
32
            }
33
        }
34
        if ( ! $source) {
2✔
35
            CLI::error('Package aplus/' . $package . ' not found');
×
36
            return;
×
37
        }
38
        $this->copyDir($source, $directory);
2✔
39
        CLI::write(
2✔
40
            $name . ' structure created at "' . $directory . '"',
2✔
41
            CLI::FG_GREEN
2✔
42
        );
2✔
43
    }
44

45
    protected function getComposerSource(string $package, bool $global = false) : false | string
46
    {
47
        $source = $global
2✔
48
            ? __DIR__ . '/../../../../../'
×
49
            : __DIR__ . '/../../';
2✔
50
        $source .= 'vendor/aplus/' . $package;
2✔
51
        if (\is_dir($source)) {
2✔
52
            return \realpath($source);
2✔
53
        }
54
        return false;
×
55
    }
56

57
    protected function getDistroSource(string $package) : false | string
58
    {
59
        $source = __DIR__ . '/../../../../packages/' . $package;
×
60
        if (\is_dir($source)) {
×
61
            return \realpath($source);
×
62
        }
63
        return false;
×
64
    }
65

66
    protected function copyDir(string $source, string $directory) : void
67
    {
68
        $iterator = new RecursiveIteratorIterator(
2✔
69
            new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
2✔
70
            RecursiveIteratorIterator::SELF_FIRST
2✔
71
        );
2✔
72
        foreach ($iterator as $item) {
2✔
73
            if ($item->isDir()) {
2✔
74
                $dir = $directory . \DIRECTORY_SEPARATOR . $iterator->getSubPathname();
2✔
75
                if ( ! \mkdir($dir, 0755, true) && ! \is_dir($dir)) {
2✔
76
                    CLI::error(
×
77
                        \sprintf('Directory "%s" could not be created', $dir)
×
78
                    );
×
79
                }
80
                continue;
2✔
81
            }
82
            \copy((string) $item, $directory . \DIRECTORY_SEPARATOR . $iterator->getSubPathname());
2✔
83
        }
84
    }
85

86
    protected function getDirectory() : string
87
    {
88
        $directory = $this->console->getArgument(0);
2✔
89
        if ($directory === null) {
2✔
90
            $directory = $this->promptDirectory();
×
91
        }
92
        if ( ! \str_starts_with($directory, '/')) {
2✔
93
            $directory = \getcwd() . '/' . $directory;
×
94
        }
95
        if (\file_exists($directory)) {
2✔
96
            CLI::error(
×
97
                \sprintf('The path "%s" already exists', $directory)
×
98
            );
×
99
        }
100
        if ( ! \mkdir($directory, 0755, true) && ! \is_dir($directory)) {
2✔
101
            CLI::error(
×
102
                \sprintf('Directory "%s" could not be created', $directory)
×
103
            );
×
104
        }
105
        $realpath = \realpath($directory);
2✔
106
        if ($realpath === false) {
2✔
107
            CLI::error(
×
108
                \sprintf('Was not possible get the realpath of "%s"', $directory)
×
109
            );
×
110
        }
111
        return $realpath; // @phpstan-ignore-line
2✔
112
    }
113

114
    protected function promptDirectory() : string
115
    {
116
        $directory = CLI::prompt('Directory');
×
117
        $directory = \trim($directory);
×
118
        if ($directory === '') {
×
119
            CLI::error('Directory path cannot be empty. Try again.', null);
×
120
            return $this->promptDirectory();
×
121
        }
122
        if ( ! \str_starts_with($directory, '/')) {
×
123
            $directory = \getcwd() . '/' . $directory;
×
124
        }
125
        if (\file_exists($directory)) {
×
126
            CLI::error('Directory already exists. Try Again.', null);
×
127
            return $this->promptDirectory();
×
128
        }
129
        return $directory;
×
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc