Exemplo n.º 1
0
 /**
  * Gets the value of an environment variable. Supports boolean, empty and null.
  *
  * @param  string $key
  * @param  mixed  $default
  *
  * @return mixed
  */
 function env($key, $default = NULL)
 {
     $value = getenv($key);
     if ($value === FALSE) {
         return value($default);
     }
     switch (strtolower($value)) {
         case 'true':
         case '(true)':
             return TRUE;
         case 'false':
         case '(false)':
             return FALSE;
         case 'empty':
         case '(empty)':
             return '';
         case 'null':
         case '(null)':
             return NULL;
     }
     if (strlen($value) > 1 && Lib::starts_with('"', $value) && Lib::ends_with('"', $value)) {
         return substr($value, 1, -1);
     }
     return $value;
 }
Exemplo n.º 2
0
 /**
  * @return array
  * @throws CollectionExportWriteFailure
  * @throws DependencyInstanceNotFound
  */
 public static function makePhpStormMeta()
 {
     static::$instance ?: new static();
     // conveniences and declarations
     $app = static::$app;
     $self = static::$instance;
     $map = [];
     $code = '';
     // collect illuminate aliases
     $forge_aliases = static::getInstance()->aliases;
     /** @var PimpleDumpProvider $pds */
     $pds = static::find('pimple_dump_service');
     $pds->dump($app);
     // get Pimple keys and merge with aliases
     $keys = $app->keys();
     // fold in forge aliases that do not already exist in the $app.
     foreach ($forge_aliases as $abstract => $alias) {
         if (class_exists($abstract)) {
             isset($keys[$abstract]) ?: ($keys[] = "\\{$abstract}");
             $map[] = "'{$alias}' instanceof \\{$abstract},";
         }
     }
     // Iterate through the key list to collect registrations.
     foreach ($keys as $key) {
         // assume nothing
         $appKey = static::key_object_exists($key) ? $self->parseValue($app, $key) : self::parseKey($app, $key);
         // ignoring 'app' replications, add the new .phpstorm.meta entry.
         if ($appKey and $appKey !== '' and $key !== 'app') {
             $map[] = "'{$key}' instanceof \\{$appKey},";
         }
     }
     // sort and build code segment
     $map = array_unique($map);
     sort($map);
     // compile the map
     foreach ($map as $entry) {
         $code .= '            ' . $entry . PHP_EOL;
     }
     $template = file_get_contents(__DIR__ . '/assets/meta.php.template');
     $template = str_replace('%%MAP%%', $code, $template);
     $template = str_replace('%%DATE%%', Carbon::now()->toDateTimeString(), $template);
     $template = str_replace('%%COUNT%%', count($map), $template);
     $template = str_replace('%%ID%%', Lib::generate_token(8, '$meta$'), $template);
     if (FALSE === file_put_contents(ROOT . '.phpstorm.meta.php', $template)) {
         throw new CollectionExportWriteFailure('Unable to update .phpstorm.meta.php.');
     }
     return $map;
 }
Exemplo n.º 3
0
 /**
  * Registers any commands found the the provided path.
  *
  * Note: All classes found in the folder are loaded, so don't put anything
  *       but commands there.
  *
  * @param string $commandPath
  *
  * @return array
  */
 private function registerCommandsIn(string $commandPath) : array
 {
     $commands = [];
     foreach ((new ClassFinder())->findClasses($commandPath) as $command) {
         $commands[] = Lib::get_class_name($command);
         $this->add(new $command());
     }
     return $commands;
 }
Exemplo n.º 4
0
 /**
  * **Directly set a key:value pair.**
  *
  * @param $key
  * @param $value
  *
  * @return void
  */
 public function set($key, $value)
 {
     # attempt writing the value to the key
     if (is_string($key)) {
         list($key, $value) = Lib::expand_segments($key, $value);
         $this->search_and_replace([$key => $value]);
     }
 }
Exemplo n.º 5
0
 /**
  * Import a key-less imported value.
  *
  * @param $value
  *
  * @return array
  * @throws \InvalidArgumentException
  */
 protected function mergeValue($value)
 {
     if (Lib::is_assoc($value)) {
         return $this->items = array_merge($this->items, $value);
     } else {
         throw new \InvalidArgumentException('Import failed due to malformed source or missing key.');
     }
 }
Exemplo n.º 6
0
 /**
  * @param string $name
  *
  * @throws DBCannotRemoveCachedConnection
  */
 public function removeConnection(string $name)
 {
     if ($this->isCached($name)) {
         throw new DBCannotRemoveCachedConnection("Connection `{$name}` is active and therefore cannot be removed.");
     }
     if ($this->hasConnection($name)) {
         Lib::array_forget($this->connections, $name);
     }
 }
Exemplo n.º 7
0
 /**
  * Get a value retrieving callback.
  *
  * @param  string $value
  *
  * @return callable
  */
 protected function valueRetriever($value)
 {
     if ($this->useAsCallable($value)) {
         return $value;
     }
     return function ($item) use($value) {
         return Lib::data_get($item, $value);
     };
 }
Exemplo n.º 8
0
 /**
  * Export the entire collection contents to a json string.
  *
  * @param int $options
  *
  * @return string
  */
 public function exportFormattedJSON($options = 0)
 {
     return Lib::encode_readable_json($this->items, $options);
 }
Exemplo n.º 9
0
 /**
  * **Invokes a class method with dependency injection.**
  *
  * Creates and optionally invokes a class->method() using dependency injection.
  *
  * @param string|object $class   - name of the class to make
  * @param string        $method  - class method to call
  * @param bool          $execute - TRUE to execute the object->method, FALSE instantiates and returns
  *
  * @return mixed - whatever the object returns
  */
 public function invokeClassMethod($class, $method = NULL, $execute = TRUE)
 {
     // is the class described by `class@method`?
     if (is_string($class) and Lib::str_has(':', $class)) {
         list($class, $method) = explode(':', $class);
     }
     // extract the route class dependencies
     $class_dependencies = $this->extract_dependencies($class, $method);
     list($reflection, $arguments) = array_values($class_dependencies);
     // construct a new class object.
     // this will trigger an early return
     if ($method === '__construct') {
         /** @var ReflectionClass $reflection */
         $reflection = new \ReflectionClass($class);
         // this is a simple call to instantiate a class.
         return $reflection->newInstanceArgs($arguments);
     }
     // optionally, transfer control over to the class:method.
     if ($execute) {
         /** @var \ReflectionClass $rf */
         $rf = new \ReflectionClass($reflection->class);
         $constructor = $class;
         // determine if a constructor exists and has required parameters
         if ($rf->getConstructor()) {
             // extract its dependencies
             $dependencies = $this->extract_dependencies($class, '__construct');
             // instantiate the object through its constructor
             $constructor = $rf->newInstanceArgs($dependencies['arg_list']);
         }
         // invoke the method
         /** @var \ReflectionMethod $reflection */
         return $reflection->invokeArgs($constructor, $arguments);
     }
     # no constructor so instantiate the class without it.
     return new $reflection->class();
 }