Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 371eba2

Browse files
committed
Allowing Ardent to be used outside of Laravel
+ included static configureAsExternal method that accepts database and translator info * using a static method to retrieve the actual Validator, based on the static property (set by the other method)
1 parent 03e82d4 commit 371eba2

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/LaravelBook/Ardent/Ardent.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111

1212

1313
use Closure;
14+
use Illuminate\Container\Container;
15+
use Illuminate\Database\Capsule\Manager as DatabaseCapsule;
16+
use Illuminate\Events\Dispatcher;
1417
use Illuminate\Support\MessageBag;
1518
use Illuminate\Support\Facades\Input;
1619
use Illuminate\Support\Facades\Hash;
1720
use Illuminate\Support\Facades\Validator;
1821
use Illuminate\Database\Eloquent\Model;
1922
use Illuminate\Support\Str;
23+
use Illuminate\Validation\Factory as ValidationFactory;
24+
use Symfony\Component\Translation\Translator;
2025

2126
/**
2227
* Ardent - Self-validating Eloquent model base class
@@ -96,6 +101,20 @@ abstract class Ardent extends Model
96101
*/
97102
public $autoHashPasswordAttributes = false;
98103

104+
/**
105+
* If set to true will try to instantiate the validator as if it was outside Laravel.
106+
*
107+
* @var bool
108+
*/
109+
protected static $externalValidator = false;
110+
111+
/**
112+
* A Translator instance, to be used by standalone Ardent instances.
113+
*
114+
* @var \Illuminate\Validation\Factory
115+
*/
116+
protected static $validationFactory;
117+
99118
/**
100119
* Create a new Ardent model instance.
101120
*
@@ -108,6 +127,31 @@ public function __construct( array $attributes = array() ) {
108127
$this->validationErrors = new MessageBag;
109128
}
110129

130+
/**
131+
* Configures Ardent to be used outside of Laravel - correctly setting Eloquent and Validation modules.
132+
*
133+
* @param array $connection Connection info used by {@link \Illuminate\Database\Capsule\Manager::addConnection}.
134+
* Should contain driver, host, port, database, username, password, charset and collation.
135+
* @param string $language A language string as used by {@link \Symfony\Component\Translation\Translator}
136+
*/
137+
public static function configureAsExternal( array $connection, $language ) {
138+
$db = new DatabaseCapsule;
139+
$db->addConnection( $connection );
140+
$db->setEventDispatcher( new Dispatcher( new Container ) );
141+
//TODO: configure a cache manager (as an option)
142+
$db->bootEloquent();
143+
144+
self::$externalValidator = true;
145+
self::$validationFactory = new ValidationFactory( new Translator( $language ) );
146+
}
147+
148+
protected static function makeValidator( $data, $rules, $customMessages ) {
149+
if (self::$externalValidator)
150+
return self::$validationFactory->make( $data, $rules, $customMessages );
151+
else
152+
return Validator::make( $data, $rules, $customMessages );
153+
}
154+
111155
/**
112156
* Validate the model instance
113157
*
@@ -142,7 +186,7 @@ public function validate( array $rules = array(), array $customMessages = array(
142186
$data = $this->getAttributes(); // the data under validation
143187

144188
// perform validation
145-
$validator = Validator::make( $data, $rules, $customMessages );
189+
$validator = self::makeValidator( $data, $rules, $customMessages );
146190
$success = $validator->passes();
147191

148192
if ( $success ) {

0 commit comments

Comments
 (0)