/**
  * @param ApplicationInterface $app
  *
  * @throws \Doctrine\ORM\ORMException
  * @throws \ObjectivePHP\Primitives\Exception
  * @throws \ObjectivePHP\ServicesFactory\Exception
  */
 public function buildEntityManagers(ApplicationInterface $app)
 {
     $entityManagers = $app->getConfig()->subset(Config\EntityManager::class);
     foreach ($entityManagers as $connection => $params) {
         if (isset($params['db'])) {
             $params = $params['db'];
         }
         // normalize if needed
         $entitiesPaths = $params['entities.locations'];
         Collection::cast($entitiesPaths)->each(function (&$path) {
             if (strpos($path, '/') !== 0) {
                 $path = getcwd() . '/' . $path;
             }
         });
         // TODO: handle isDev depending on app config
         $emConfig = Setup::createAnnotationMetadataConfiguration((array) $entitiesPaths, true);
         $emConfig->setNamingStrategy(new UnderscoreNamingStrategy());
         $em = EntityManager::create($params, $emConfig);
         if (!empty($params['mapping_types']) && is_array($params['mapping_types'])) {
             $platform = $em->getConnection()->getDatabasePlatform();
             foreach ($params['mapping_types'] as $type => $mapping) {
                 if (!Type::hasType($type) && class_exists($mapping)) {
                     Type::addType($type, $mapping);
                     $mapping = $type;
                 }
                 $platform->registerDoctrineTypeMapping($type, $mapping);
             }
         }
         // register entity manager as a service
         $emServiceId = 'doctrine.em.' . Str::cast($connection)->lower();
         $app->getServicesFactory()->registerService(['id' => $emServiceId, 'instance' => $em]);
         $app->getServicesFactory()->registerService(['id' => 'db.connection.' . $connection, 'instance' => $em->getConnection()->getWrappedConnection()]);
     }
 }
 /**
  * @return Str
  */
 public function getMessage($code, $values = [])
 {
     $message = Str::cast($this->getMessages()->get($code));
     foreach ($values as $placeHolder => $value) {
         $message->setVariable($placeHolder, $value);
     }
     return $message;
 }
Ejemplo n.º 3
0
 public function testPreviousEventReferenceAccessorAndMutator()
 {
     $previousEvent = new Event();
     $previousEvent->setName('previous.event');
     $currentEvent = new Event();
     $currentEvent->setName('current.event')->setPrevious($previousEvent);
     $this->assertInstanceOf(Event::class, $currentEvent->getPrevious());
     $this->assertEquals(Str::cast('previous.event'), $currentEvent->getPrevious()->getName());
 }
Ejemplo n.º 4
0
 protected function renderException(\Throwable $exception)
 {
     $div = Tag::div(Tag::h2('Exception trace'), 'errors');
     $div->append(Tag::h2('Exception'), Tag::i(get_class($exception)));
     $div->append(Tag::h2('Message'), Tag::pre($exception->getMessage()));
     $div->append(Tag::h2('File'), Tag::pre($exception->getFile())->append(':', $exception->getLine())->setSeparator(''));
     // shorten Trace
     $trace = Str::cast($exception->getTraceAsString())->replace(getcwd() . '/', '');
     $div->append(Tag::h2('Trace'), Tag::pre($trace));
     return $div;
 }
Ejemplo n.º 5
0
 /**
  * @param $path
  *
  * @return string
  */
 protected function resolveActionClassName($path)
 {
     // clean path name
     $path = Str::cast($path);
     $path->trim('/');
     $namespaces = $path->split('/');
     $namespaces->each(function (&$namespace) {
         $parts = explode('-', $namespace);
         array_walk($parts, function (&$part) {
             $part = ucfirst($part);
         });
         $namespace = implode('', $parts);
     });
     $backslash = '\\';
     $className = str_replace('\\\\', '\\', implode($backslash, $namespaces->toArray()));
     return $className;
 }
Ejemplo n.º 6
0
 /**
  * Return normalized service name reflecting path
  *
  * This will use to auto-register action as a service
  *
  * @param $path
  */
 protected function computeServiceName($path)
 {
     return (string) Str::cast($path)->trim('/')->replace('/', '.')->prepend('action.');
 }
 /**
  * @param string $message
  *
  * @return $this
  */
 public function setMessage($code, $message)
 {
     $this->messages[$code] = Str::cast($message);
     return $this;
 }
 /**
  * @param Str $id
  *
  * @return $this
  */
 public function setId($id)
 {
     $this->id = Str::cast($id);
     return $this;
 }
Ejemplo n.º 9
0
 public function __construct($message)
 {
     $this->message = Str::cast($message);
 }
Ejemplo n.º 10
0
 /**
  * @param $reference
  *
  * @return \ObjectivePHP\Primitives\String\Str
  */
 public static function string($reference)
 {
     return Str::cast(self::get($reference));
 }
 /**
  * @param $value
  *
  * @return int
  */
 public function process($value)
 {
     return Str::cast($value);
 }
 /**
  *
  * @param array $servicesSpecs
  *
  * @return $this
  * @throws Exception
  */
 public function registerService(...$servicesSpecs)
 {
     foreach ($servicesSpecs as $serviceSpecs) {
         // if service specs is not an instance of ServiceSpecsInterface,
         // try to build the specs using factory
         if (!$serviceSpecs instanceof ServiceSpecsInterface) {
             try {
                 $serviceSpecs = AbstractServiceSpecs::factory($serviceSpecs);
             } catch (\Exception $e) {
                 throw new Exception(AbstractServiceSpecs::class . '::factory() was unable to build service specifications', Exception::INVALID_SERVICE_SPECS, $e);
             }
         }
         if (!$serviceSpecs instanceof ServiceSpecsInterface) {
             // the specs are still not valid
             throw new Exception('Service specifications are not an instance of ' . ServiceSpecsInterface::class, Exception::INVALID_SERVICE_SPECS);
         }
         $serviceId = Str::cast($serviceSpecs->getId())->lower();
         // prevent final services from being overridden
         if ($previouslyRegistered = $this->getServiceSpecs((string) $serviceId)) {
             // a service with same name already has been registered
             if ($previouslyRegistered->isFinal()) {
                 // as it is marked as final, it cannot be overridden
                 throw new Exception(sprintf('Cannot override service "%s" as it has been registered as a final service', $serviceId), Exception::FINAL_SERVICE_OVERRIDING_ATTEMPT);
             }
         }
         // store the service specs for further reference
         $this->services[(string) $serviceId] = $serviceSpecs;
     }
     return $this;
 }
Ejemplo n.º 13
0
 public function testStrToSnakeCase()
 {
     $string = new Str('camelCase');
     $this->assertEquals('camel_case', $string->snakeCase());
 }
Ejemplo n.º 14
0
 /**
  * Event name setter
  *
  * @param string $name The event name
  */
 public function setName($name)
 {
     $name = Str::cast($name);
     $this->name = $name->lower();
     return $this;
 }