Skip to content

Commit 61a4a87

Browse files
update
1 parent a2a7154 commit 61a4a87

31 files changed

+839
-264
lines changed

README.md

Lines changed: 122 additions & 96 deletions
Large diffs are not rendered by default.

README2.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Tamedevelopers Database CLI
2+
3+
## Quick start
4+
5+
1. Install dependencies
6+
```bash
7+
composer install
8+
```
9+
10+
2. List commands
11+
```bash
12+
php database list
13+
# or via composer bin (after composer install)
14+
./vendor/bin/database list
15+
```
16+
17+
3. Scaffold example table
18+
```bash
19+
php database scaffold --name=posts
20+
```
21+
22+
## Adding new commands
23+
- Create a new class in `src/Console/Commands`, e.g. `MakeModelCommand.php` with a `handle(array $args): int` method.
24+
- Register it in `src/Console/Artisan.php` constructor:
25+
```php
26+
$this->register('make:model', [new MakeModelCommand(), 'handle']);
27+
```
28+
29+
## Notes
30+
- Commands run in CLI, no manual browser reload needed.
31+
- Env and logger are booted automatically in the sample command.

composer.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717
],
1818
"require": {
1919
"php": ">=8.0",
20-
"tamedevelopers/support": "*"
20+
"tamedevelopers/support": "^5.0",
21+
"symfony/console": "^6.0"
22+
},
23+
"scripts": {
24+
"post-install-cmd": [
25+
"Tamedevelopers\\Database\\Installer::postInstall"
26+
],
27+
"post-update-cmd": [
28+
"Tamedevelopers\\Database\\Installer::postUpdate"
29+
]
2130
},
2231
"autoload": {
2332
"files": [

src/Auth.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tamedevelopers\Support\Hash;
99
use Tamedevelopers\Support\Capsule\Manager;
1010
use Tamedevelopers\Database\Traits\AuthTrait;
11+
use Tamedevelopers\Database\Traits\ExceptionTrait;
1112

1213
/**
1314
* Class Auth Manager
@@ -22,7 +23,7 @@
2223
*/
2324
class Auth
2425
{
25-
use AuthTrait;
26+
use AuthTrait, ExceptionTrait;
2627

2728
/**
2829
* Instance of Database Object
@@ -44,14 +45,14 @@ class Auth
4445
/**
4546
* The table name associated with the current guard.
4647
*
47-
* @var string|null $table
48+
* @var string|null
4849
*/
4950
protected $table;
5051

5152
/**
5253
* Session key used to store authenticated user data.
5354
*
54-
* @var string $session
55+
* @var string
5556
*/
5657
protected static $session = 'tame_auth_user';
5758

@@ -63,6 +64,7 @@ class Auth
6364
*/
6465
public function __construct($table = null, $connection = null)
6566
{
67+
// Ensure environment variables are loaded before accessing them
6668
Manager::startEnvIFNotStarted();
6769

6870
$this->table = $table;
@@ -139,6 +141,16 @@ public function login($userData = null, bool $persist = true): void
139141
}
140142
}
141143

144+
/**
145+
* Determine if the current user is a guest.
146+
*
147+
* @return bool
148+
*/
149+
public function guest()
150+
{
151+
return !$this->check();
152+
}
153+
142154
/**
143155
* Get the currently authenticated user (from memory or session).
144156
*
@@ -196,7 +208,13 @@ public function logout(): void
196208
public function __get(string $name)
197209
{
198210
if ($name === 'user') {
199-
throw new \RuntimeException("Direct access to 'user' is restricted. Use user() method instead.");
211+
$className = get_class($this);
212+
try {
213+
throw new \RuntimeException("
214+
Cannot access protected property {$className}::$$name. Use user() method instead.");
215+
} catch (\Throwable $th) {
216+
$this->errorException($th);
217+
}
200218
}
201219
return null;
202220
}

src/AutoLoader.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,60 @@
88
use Tamedevelopers\Database\Traits\AutoLoaderTrait;
99
use Tamedevelopers\Database\Schema\Pagination\PaginatorAsset;
1010

11-
class AutoLoader{
12-
11+
class AutoLoader
12+
{
1313
use AutoLoaderTrait;
1414

1515
protected static $default;
16+
17+
/**
18+
* Boot the AutoLoader::start.
19+
* If the constant 'TAME_AUTOLOADER_BOOT' is not defined,
20+
* it defines it and starts the debugger automatically
21+
*
22+
* So that this is only called once in entire application life-cycle
23+
*
24+
* @param string|null $path
25+
* @param bool $createDummy
26+
* @return void
27+
*/
28+
public static function boot($path = null, $createDummy = true)
29+
{
30+
if(!defined('TAME_AUTOLOADER_BOOT')){
31+
// start auto loader
32+
self::start($path, $createDummy);
33+
34+
// Define boot logger as true
35+
define('TAME_AUTOLOADER_BOOT', 1);
36+
}
37+
}
1638

1739
/**
1840
* Star env configuration
1941
*
20-
* @param string|null $custom_path
42+
* @param string|null $path
2143
* path \Path to .env file
2244
* - [optional] path \By default we use project root path
2345
*
46+
* @param bool $createDummy
47+
*
2448
* @return void
2549
*/
26-
public static function start($custom_path = null)
50+
public static function start($path = null, $createDummy = true)
2751
{
2852
/*
2953
|--------------------------------------------------------------------------
3054
| Instance of class
3155
|--------------------------------------------------------------------------
3256
*/
33-
$Env = new Env($custom_path);
57+
$env = new Env($path);
3458

3559
/*
3660
|--------------------------------------------------------------------------
3761
| Create a sample .env file if not exist in project
3862
|--------------------------------------------------------------------------
3963
*/
40-
$Env::createOrIgnore();
64+
$env::createOrIgnore();
4165

4266
/*
4367
|--------------------------------------------------------------------------
@@ -47,25 +71,16 @@ public static function start($custom_path = null)
4771
| or exit with error status code
4872
|
4973
*/
50-
$Env::loadOrFail();
51-
52-
/*
53-
|--------------------------------------------------------------------------
54-
| Storing data into a Constant once everything is successful
55-
|--------------------------------------------------------------------------
56-
| We can now use on anywhere on our application
57-
| Mostly to get our defined .env root Path
58-
*/
59-
if ( ! defined('TAME_SERVER_CONNECT') ) {
60-
define('TAME_SERVER_CONNECT', $Env->getServers());
61-
}
74+
$env::loadOrFail();
6275

6376
/*
6477
|--------------------------------------------------------------------------
6578
| Automatically create dummy files
6679
|--------------------------------------------------------------------------
6780
*/
68-
self::createDummy(realpath(__DIR__));
81+
if($createDummy){
82+
self::createDummy(realpath(__DIR__));
83+
}
6984
}
7085

7186
/**

src/Capsule/AppManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function bootLoader()
4646
| path => \storage\logs\orm.log
4747
|--------------------------------------------------------------------------
4848
*/
49-
Env::bootLogger();
49+
Env::boot();
5050

5151
/*
5252
|--------------------------------------------------------------------------
@@ -59,10 +59,10 @@ public static function bootLoader()
5959
/*
6060
|--------------------------------------------------------------------------
6161
| Start env configuration
62-
| You can configura your pagination text data here if you like
62+
| You can configure your pagination text data here if you like
6363
|--------------------------------------------------------------------------
6464
*/
65-
AutoLoader::start();
65+
AutoLoader::boot();
6666
}
6767

6868
}

src/Connectors/Connector.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
use Tamedevelopers\Support\Capsule\Manager;
1212
use Tamedevelopers\Database\DatabaseManager;
1313
use Tamedevelopers\Support\Capsule\FileCache;
14+
use Tamedevelopers\Database\Traits\ExceptionTrait;
1415
use Tamedevelopers\Database\Schema\Pagination\Paginator;
1516
use Tamedevelopers\Database\Connectors\ConnectionBuilder;
1617
use Tamedevelopers\Database\Schema\Traits\ExpressionTrait;
1718
use Tamedevelopers\Database\Connectors\Traits\ConnectorTrait;
1819

1920

20-
class Connector {
21+
class Connector extends DatabaseManager{
2122

2223
use ConnectorTrait,
24+
ExceptionTrait,
2325
ExpressionTrait;
2426

2527
/**
@@ -68,7 +70,7 @@ public function __construct($name = null, $connection = null, $data = [])
6870
*/
6971
static public function addConnection($name = null, $connection = null, $data = [])
7072
{
71-
$driver = DatabaseManager::driverValidator($name);
73+
$driver = static::driverValidator($name);
7274

7375
// driver name
7476
$driverName = $driver['name'];
@@ -92,7 +94,7 @@ static public function addConnection($name = null, $connection = null, $data = [
9294
*/
9395
static public function removeFromConnection($name = null)
9496
{
95-
$driver = DatabaseManager::driverValidator($name);
97+
$driver = static::driverValidator($name);
9698

9799
// driver name
98100
$driverName = $driver['name'];
@@ -212,7 +214,14 @@ private function buidTable($table = null)
212214

213215
// There's no connecton instance set
214216
if(empty($instance)){
215-
throw new Exception("There's no active connection! Unknown connection [{$this->name}].");
217+
try {
218+
throw new Exception("
219+
There's no active connection! Unknown connection [{$this->name}]. \n\n
220+
Use DB::connection(\$connName), to instatiate connection.
221+
");
222+
} catch (\Throwable $th) {
223+
$this->errorException($th);
224+
}
216225
}
217226

218227
// set connection
@@ -314,7 +323,7 @@ public function getConfig()
314323
private static function getConnectionFromDatabaseFile($name = null, $default = [])
315324
{
316325
$data = Server::config(
317-
DatabaseManager::getConnectionKey($name),
326+
static::getConnectionKey($name),
318327
[]
319328
);
320329

@@ -381,9 +390,9 @@ private function isModelDriverCreated()
381390
{
382391
if(self::isModelExtended()){
383392
$this->setConnectionName();
384-
$key = DatabaseManager::getConnectionKey($this->name);
393+
$key = static::getConnectionKey($this->name);
385394
if (!FileCache::exists($key)) {
386-
DatabaseManager::connection($this->name);
395+
static::connection($this->name);
387396
}
388397
}
389398
}

src/Connectors/Traits/ConnectorTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Tamedevelopers\Database\Connectors\PostgresConnector;
1212

1313
trait ConnectorTrait{
14-
14+
1515
/**
1616
* Check if config key is isset and not empty
1717
*

0 commit comments

Comments
 (0)