You are here

public function SessionHelper::processPath in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/stream_wrapper_example/src/SessionHelper.php \Drupal\stream_wrapper_example\SessionHelper::processPath()

Turn a path into the arrays we use internally.

Parameters

string $path: Path into the store.

bool $is_dir: Path will be used as a container. Otherwise, path is a scalar.

Return value

array|bool Return an array containing the "bottom" and "tip" of a directory hierarchy. You will want to save the 'bottom' array, but you may need to manipulate an object at the very tip of the hierarchy as defined in the path. The tip will be a string if we are scalar and an array otherwise. Since we don't want to create new sub arrays as a side effect, we return FALSE the intervening path does not exist.

4 calls to SessionHelper::processPath()
SessionHelper::checkPath in stream_wrapper_example/src/SessionHelper.php
Does path exist?
SessionHelper::clearPath in stream_wrapper_example/src/SessionHelper.php
Clear a path into our store.
SessionHelper::getPath in stream_wrapper_example/src/SessionHelper.php
Get a path.
SessionHelper::setPath in stream_wrapper_example/src/SessionHelper.php
Set a path.

File

stream_wrapper_example/src/SessionHelper.php, line 93

Class

SessionHelper
Helper to manage file wrapper data stored in the session object.

Namespace

Drupal\stream_wrapper_example

Code

public function processPath($path, $is_dir = FALSE) {

  // We need to create a reference into the store for the point
  // the of the path, so get a copy of the store.
  $store = $this
    ->getStore();
  if (empty($path)) {
    return [
      'store' => &$store,
      'tip' => &$store,
    ];
  }
  $hierarchy = explode('/', $path);
  if (empty($hierarchy) or empty($hierarchy[0])) {
    return [
      'store' => &$store,
      'tip' => &$store,
    ];
  }
  $bottom =& $store;
  $tip = array_pop($hierarchy);
  foreach ($hierarchy as $dir) {
    if (!isset($bottom[$dir])) {

      // If the path does not exist, DO NOT create it.
      // That is handled by the stream wrapper code.
      return FALSE;
    }
    $new_tip =& $bottom[$dir];
    $bottom =& $new_tip;
  }

  // If the hierarchy was empty, just point to the object.
  $new_tip =& $bottom[$tip];
  $bottom =& $new_tip;
  return [
    'store' => &$store,
    'tip' => &$bottom,
  ];
}