yii\BaseYii::getAlias PHP Method

getAlias() public static method

The translation is done according to the following procedure: 1. If the given alias does not start with '@', it is returned back without change; 2. Otherwise, look for the longest registered alias that matches the beginning part of the given alias. If it exists, replace the matching part of the given alias with the corresponding registered path. 3. Throw an exception or return false, depending on the $throwException parameter. For example, by default '@yii' is registered as the alias to the Yii framework directory, say '/path/to/yii'. The alias '@yii/web' would then be translated into '/path/to/yii/web'. If you have registered two aliases '@foo' and '@foo/bar'. Then translating '@foo/bar/config' would replace the part '@foo/bar' (instead of '@foo') with the corresponding registered path. This is because the longest alias takes precedence. However, if the alias to be translated is '@foo/barbar/config', then '@foo' will be replaced instead of '@foo/bar', because '/' serves as the boundary character. Note, this method does not check if the returned path exists or not.
See also: setAlias()
public static getAlias ( string $alias, boolean $throwException = true ) : string | boolean
$alias string the alias to be translated.
$throwException boolean whether to throw an exception if the given alias is invalid. If this is false and an invalid alias is given, false will be returned by this method.
return string | boolean the path corresponding to the alias, false if the root alias is not previously registered.
    public static function getAlias($alias, $throwException = true)
    {
        if (strncmp($alias, '@', 1)) {
            // not an alias
            return $alias;
        }
        $pos = strpos($alias, '/');
        $root = $pos === false ? $alias : substr($alias, 0, $pos);
        if (isset(static::$aliases[$root])) {
            if (is_string(static::$aliases[$root])) {
                return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos);
            } else {
                foreach (static::$aliases[$root] as $name => $path) {
                    if (strpos($alias . '/', $name . '/') === 0) {
                        return $path . substr($alias, strlen($name));
                    }
                }
            }
        }
        if ($throwException) {
            throw new InvalidParamException("Invalid path alias: {$alias}");
        } else {
            return false;
        }
    }

Usage Example

Example #1
0
 /**
  * This command echoes what you have entered as the message.
  * @param string $message the message to be echoed.
  */
 public function actionIndex()
 {
     $includeFiles = getenv('INCLUDE_FILES');
     if ($includeFiles) {
         $includeFiles = explode(',', $includeFiles);
         foreach ($includeFiles as $file) {
             require_once $file;
         }
     }
     if (file_exists(Yii::getAlias('@app') . '/config/console.php')) {
         // Yii2-Basic
         $config = (require Yii::getAlias('@app') . '/config/console.php');
     } else {
         // Yii2-Advance
         $config = (require Yii::getAlias('@app') . '/config/main.php');
     }
     $application = new \yii\console\Application($config);
     # Turn off our amazing library autoload
     spl_autoload_unregister(array('Yii', 'autoload'));
     if (file_exists(Yii::getAlias('@vendor') . '/resque/yii2-resque/ResqueAutoloader.php')) {
         // Yii2-Basic
         require_once Yii::getAlias('@vendor') . '/resque/yii2-resque/ResqueAutoloader.php';
     } else {
         // Yii2-Advance
         require_once Yii::getAlias('@app') . '/../vendor/resque/yii2-resque/ResqueAutoloader.php';
     }
     ResqueAutoloader::register();
     # Give back the power to Yii
     spl_autoload_register(array('Yii', 'autoload'));
     $QUEUE = getenv('QUEUE');
     if (empty($QUEUE)) {
         die("Set QUEUE env var containing the list of queues to work.\n");
     }
     $REDIS_BACKEND = getenv('REDIS_BACKEND');
     $REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
     $REDIS_AUTH = getenv('REDIS_AUTH');
     if (!empty($REDIS_BACKEND)) {
         $REDIS_BACKEND_DB = !empty($REDIS_BACKEND_DB) ? $REDIS_BACKEND_DB : 0;
         Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB, $REDIS_AUTH);
     }
     $logLevel = 0;
     $LOGGING = getenv('LOGGING');
     $VERBOSE = getenv('VERBOSE');
     $VVERBOSE = getenv('VVERBOSE');
     if (!empty($LOGGING) || !empty($VERBOSE)) {
         $logLevel = Resque_Worker::LOG_NORMAL;
     } else {
         if (!empty($VVERBOSE)) {
             $logLevel = Resque_Worker::LOG_VERBOSE;
         }
     }
     $logger = null;
     $LOG_HANDLER = getenv('LOGHANDLER');
     $LOG_HANDLER_TARGET = getenv('LOGHANDLERTARGET');
     if (class_exists('MonologInit_MonologInit')) {
         if (!empty($LOG_HANDLER) && !empty($LOG_HANDLER_TARGET)) {
             $logger = new MonologInit_MonologInit($LOG_HANDLER, $LOG_HANDLER_TARGET);
         } else {
             fwrite(STDOUT, '*** loghandler or logtarget is not set.' . "\n");
         }
     } else {
         fwrite(STDOUT, '*** MonologInit_MonologInit logger cannot be found, continue without loghandler.' . "\n");
     }
     $interval = 5;
     $INTERVAL = getenv('INTERVAL');
     if (!empty($INTERVAL)) {
         $interval = $INTERVAL;
     }
     $count = 1;
     $COUNT = getenv('COUNT');
     if (!empty($COUNT) && $COUNT > 1) {
         $count = $COUNT;
     }
     $PREFIX = getenv('PREFIX');
     if (!empty($PREFIX)) {
         fwrite(STDOUT, '*** Prefix set to ' . $PREFIX . "\n");
         Resque::redis()->prefix($PREFIX);
     }
     if ($count > 1) {
         for ($i = 0; $i < $count; ++$i) {
             $pid = Resque::fork();
             if ($pid == -1) {
                 die("Could not fork worker " . $i . "\n");
             } else {
                 if (!$pid) {
                     startWorker($QUEUE, $logLevel, $logger, $interval);
                     break;
                 }
             }
         }
     } else {
         $PIDFILE = getenv('PIDFILE');
         if ($PIDFILE) {
             file_put_contents($PIDFILE, getmypid()) or die('Could not write PID information to ' . $PIDFILE);
         }
         $this->startWorker($QUEUE, $logLevel, $logger, $interval);
     }
 }