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

tattersoftware / codeigniter4-schemas / 3884177658

pending completion
3884177658

Pull #44

github

GitHub
Merge dbcb0d181 into cee97dbe4
Pull Request #44: fix: ForeignKey should return strings

6 of 6 new or added lines in 2 files covered. (100.0%)

6 existing lines in 2 files now uncovered.

397 of 519 relevant lines covered (76.49%)

5.28 hits per line

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

10.81
/src/Commands/Schemas.php
1
<?php
2

3
namespace Tatter\Schemas\Commands;
4

5
use CodeIgniter\CLI\BaseCommand;
6
use CodeIgniter\CLI\CLI;
7
use Exception;
8
use Tatter\Schemas\Exceptions\SchemasException;
9

10
class Schemas extends BaseCommand
11
{
12
    protected $group       = 'Database';
13
    protected $name        = 'schemas';
14
    protected $description = 'Manage database schemas.';
15
    protected $usage       = 'schemas [-draft handler1,handler2,...] [-archive handler1,... | -print]';
16
    protected $options     = [
17
        '-draft'   => 'Handler(s) for drafting the schema ("database", "model", etc)',
18
        '-archive' => 'Handler(s) for archiving a copy of the schema',
19
        '-print'   => 'Print out the drafted schema',
20
    ];
21

22
    public function run(array $params)
23
    {
24
        // Always use a clean library with automation disabled
25
        $config           = config('Schemas');
×
26
        $config->automate = [
×
UNCOV
27
            'draft'   => false,
×
UNCOV
28
            'archive' => false,
×
UNCOV
29
            'read'    => false,
×
UNCOV
30
        ];
×
31
        $schemas = new \Tatter\Schemas\Schemas($config, null);
×
32

33
        // Determine draft handlers
34
        if ($drafters = $params['-draft'] ?? CLI::getOption('draft')) {
×
35
            $drafters = explode(',', $drafters);
×
36

37
            // Map each name to its handler
38
            $drafters = array_map([$this, 'getDraftHandler'], $drafters);
×
39
        } else {
40
            $drafters = $config->draftHandlers;
×
41
        }
42

43
        // Determine archive handlers
44
        if ($params['-print'] ?? CLI::getOption('print')) {
×
45
            $archivers = '\Tatter\Schemas\Archiver\Handlers\CliHandler';
×
46
        } elseif ($archivers = $params['-archive'] ?? CLI::getOption('archive')) {
×
47
            $archivers = explode(',', $archivers);
×
48

49
            // Map each name to its handler
50
            $archivers = array_map([$this, 'getArchiveHandler'], $archivers);
×
51
        } else {
52
            $archivers = $config->archiveHandlers;
×
53
        }
54

55
        // Try the draft
56
        try {
57
            $schemas->draft($drafters);
×
58
        } catch (Exception $e) {
×
59
            $this->showError($e);
×
60
        }
61

62
        // Try the archive
63
        try {
64
            $result = $schemas->archive($archivers);
×
65
        } catch (Exception $e) {
×
66
            $this->showError($e);
×
67
        }
68

69
        if (empty($result)) {
×
70
            CLI::write('Archive failed!', 'red');
×
71

72
            foreach ($schemas->getErrors() as $error) {
×
73
                CLI::write($error, 'yellow');
×
74
            }
75

76
            return;
×
77
        }
78

79
        CLI::write('success', 'green');
×
80
    }
81

82
    /**
83
     * Try to match a shorthand name to its full handler class
84
     *
85
     * @param string $type The type of handler (drafter, archiver, etc)
86
     * @param string $name The name of the handler
87
     */
88
    protected function getHandler(string $type, string $name): string
89
    {
90
        // Check if it is already namespaced
91
        if (strpos($name, '\\') !== false) {
1✔
92
            return $name;
×
93
        }
94

95
        $class = '\Tatter\Schemas\\' . $type . '\Handlers\\' . ucfirst($name) . 'Handler';
1✔
96

97
        if (! class_exists($class)) {
1✔
98
            throw SchemasException::forUnsupportedHandler($name);
×
99
        }
100

101
        return $class;
1✔
102
    }
103

104
    /**
105
     * Helper for getHandler
106
     *
107
     * @param string $name The name of the handler
108
     */
109
    protected function getDraftHandler(string $name): string
110
    {
111
        return $this->getHandler('Drafter', $name);
×
112
    }
113

114
    /**
115
     * Helper for getHandler
116
     *
117
     * @param string $name The name of the handler
118
     */
119
    protected function getArchiveHandler(string $name): string
120
    {
121
        return $this->getHandler('Archiver', $name);
×
122
    }
123
}
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