Skip to content

Commit 48e9f90

Browse files
Default Tame-Artisan Command added
1 parent 9f01424 commit 48e9f90

File tree

7 files changed

+118
-5
lines changed

7 files changed

+118
-5
lines changed

Capsule/Artisan.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Tamedevelopers\Support\Capsule\Logger;
88
use Tamedevelopers\Support\Capsule\Manager;
9+
use Tamedevelopers\Support\Capsule\CommandHelper;
910
use Tamedevelopers\Support\Capsule\Traits\ArtisanTrait;
1011
use Tamedevelopers\Support\Capsule\Traits\ArtisanDiscovery;
1112

@@ -18,7 +19,7 @@
1819
* php tame key:generate
1920
* php tame migrate:fresh --seed --database=mysql
2021
*/
21-
class Artisan
22+
class Artisan extends CommandHelper
2223
{
2324
use ArtisanTrait,
2425
ArtisanDiscovery;

Capsule/CommandHelper.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ protected function hasOption(array $options, string $key): bool
9898
return !empty($options[$key]);
9999
}
100100

101+
/**
102+
* Extract table name from a migration string like "create_users_table".
103+
*
104+
* @param string|null $migration
105+
* @return string|null
106+
*/
107+
protected function extractTableName($migration = null)
108+
{
109+
// Ensure it matches the "create_*_table" pattern
110+
if (preg_match('/^create_(.+)_table$/', Str::lower($migration), $matches)) {
111+
return $matches[1]; // middle part (e.g., "users")
112+
}
113+
114+
return null; // not a valid pattern
115+
}
116+
101117
/**
102118
* Prompt the user for confirmation (y/n).
103119
*/
@@ -118,7 +134,12 @@ protected function confirm(string $question, bool $default = false): bool
118134
*/
119135
protected function ask(string $question, string $default = ''): string
120136
{
121-
$answer = readline("{$question} ");
137+
// Print the question and force a new line
138+
echo $question . PHP_EOL . "> ";
139+
140+
// Now capture user input
141+
$answer = trim(readline());
142+
122143
return $answer !== '' ? $answer : $default;
123144
}
124145

Capsule/Logger.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ protected static function registerDefaultStyles(ConsoleOutput $output): void
7373
'success_header' => ['bright-white', 'green', ['bold']],
7474
'info_header' => ['bright-white', 'cyan', ['bold']],
7575

76+
// generic formatting helpers
77+
'bold' => [null, null, ['bold']],
78+
'underline' => [null, null, ['underscore']],
79+
'dim' => ['gray', null, []],
80+
7681
// extras
7782
'yellow' => ['yellow', null, ['bold']],
7883
'green' => ['green', null, ['bold']],
@@ -109,9 +114,13 @@ public static function addStyle(string $name, string $foreground, ?string $backg
109114

110115
/**
111116
* Generic write with optional STDERR target.
117+
* Supports common aliases like <bold>, <b>, <strong>, <i>, <em>, <u>, <underline>, <dim>.
112118
*/
113119
public static function writeln(string $message, bool $stderr = false): void
114120
{
121+
// Normalize popular formatting tags to Symfony Console-supported tags
122+
$message = static::normalizeFormatting($message);
123+
115124
$out = static::getOutput();
116125
$target = $stderr ? $out->getErrorOutput() : $out;
117126
$target->writeln($message);
@@ -140,6 +149,37 @@ public static function segments(array $segments, string $separator = ''): string
140149
return implode($separator, $parts);
141150
}
142151

152+
/**
153+
* Normalize common/alias formatting tags to supported console styles.
154+
* - Maps: <b>, <strong>, <bold> => <bold>
155+
* <u>, <underline> => <underline>
156+
* <dim> => <dim>
157+
* - Strips unsupported emphasis tags like <i>, <em> (keeps content).
158+
*/
159+
protected static function normalizeFormatting(string $message): string
160+
{
161+
$map = [
162+
'b' => 'bold',
163+
'strong' => 'bold',
164+
'bold' => 'bold',
165+
'u' => 'underline',
166+
'underline' => 'underline',
167+
'dim' => 'dim',
168+
];
169+
170+
foreach ($map as $from => $to) {
171+
// Opening tags
172+
$message = preg_replace('/<\s*' . $from . '\s*>/i', '<' . $to . '>', $message);
173+
// Closing tags
174+
$message = preg_replace('/<\s*\/\s*' . $from . '\s*>/i', '</' . $to . '>', $message);
175+
}
176+
177+
// Remove unsupported tags but keep their content
178+
$message = preg_replace('/<\s*\/?\s*(i|em)\s*>/i', '', $message);
179+
180+
return $message;
181+
}
182+
143183
/**
144184
* Print a group/section header (e.g., a namespace or package name).
145185
* Accepts optional style/color while keeping backward compatibility.

Commands/MakeCommand.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Support\Commands;
6+
7+
8+
use Tamedevelopers\Support\Capsule\Logger;
9+
use Tamedevelopers\Support\Capsule\CommandHelper;
10+
11+
12+
class MakeCommand extends CommandHelper
13+
{
14+
/**
15+
* Default entry when running command
16+
*/
17+
public function handle(array $args = [], array $options = []): int
18+
{
19+
echo "Usage examples:\n";
20+
echo " php tame make\n";
21+
echo " php tame make:command [name] --path=users\n\n";
22+
return 0;
23+
}
24+
25+
/**
26+
* Create a new [Tame-Artisan] command
27+
*/
28+
public function command(array $args = [], array $options = []): int
29+
{
30+
$name = $args[0] ?? null;
31+
$path = $this->getOption($options, 'path');
32+
33+
// if not provided, prompt for file name
34+
if(empty($name)){
35+
$name = $this->ask("\nWhat should the command be named?");
36+
}
37+
38+
Logger::info("Default Artisan command <b>[{$name}]</b>: coming soon!\n");
39+
exit(1);
40+
}
41+
42+
}

Installer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected static function publishDefaults()
6262
// create for [tame]
6363
self::createTameBash($paths);
6464

65-
Logger::info("\n<Tame-Artisan> has been created automatically!\n\nUsage: \n php tame <command> [:option] [arguments]\n\n");
65+
Logger::info("\n<b>[Tame-Artisan]</b> has been created automatically!\n\nUsage: \n php tame <command> [:option] [arguments]\n\n");
6666
}
6767
}
6868
}

Process/HttpRequest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ public static function full(): string
186186
return self::http() . self::host() . self::path();
187187
}
188188

189+
/**
190+
* Determine if the script is running in CLI mode.
191+
*
192+
* @return bool
193+
*/
194+
public static function runningInConsole()
195+
{
196+
return (php_sapi_name() === 'cli' || PHP_SAPI === 'cli');
197+
}
198+
189199
/**
190200
* Is IP accessed via 127.0.0.1 port in browser
191201
*

Time.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ public function format($format = null, $date = null)
347347
{
348348
if(!empty($date)){
349349
$clone = $this->__setDate($date);
350-
351-
$this->date = $this->date;
350+
$this->date = $clone->date;
352351
}
353352

354353
if(empty($format)){

0 commit comments

Comments
 (0)