doParse($item); return $aliasName; } /** * Creae a SiteAliasName object from an alias name string. * * @param string $sitename The alias name for the site. * @param string $env The name for the site's environment. */ public function __construct($sitename = null, $env = null) { $this->sitename = $sitename; $this->env = $env; } /** * Convert an alias name back to a string. * * @return string */ public function __toString() { $parts = [ $this->sitename() ]; if ($this->hasEnv()) { $parts[] = $this->env(); } return '@' . implode('.', $parts); } /** * Determine whether or not the provided name is an alias name. * * @param string $aliasName * @return bool */ public static function isAliasName($aliasName) { // Alias names provided by users must begin with '@' if (empty($aliasName) || ($aliasName[0] != '@')) { return false; } return preg_match(self::ALIAS_NAME_REGEX, $aliasName); } /** * Return the sitename portion of the alias name. By definition, * every alias must have a sitename. If the site name is implicit, * then 'self' is assumed. * * @return string */ public function sitename() { return empty($this->sitename) ? 'self' : $this->sitename; } /** * Set the sitename portion of the alias name * * @param string $sitename */ public function setSitename($sitename) { $this->sitename = $sitename; } /** * In general, all aliases have a sitename. The time when one will not * is when an environment name `@env` is used as a shortcut for `@self.env` * * @return bool */ public function hasSitename() { return !empty($this->sitename); } /** * Return true if this alias name contains an 'env' portion. * * @return bool */ public function hasEnv() { return !empty($this->env); } /** * Set the environment portion of the alias record. * * @param string */ public function setEnv($env) { $this->env = $env; } /** * Return the 'env' portion of the alias record. * * @return string */ public function env() { return $this->env; } /** * Return true if this alias name is the 'self' alias. * * @return bool */ public function isSelf() { return ($this->sitename == 'self') && !isset($this->env); } /** * Return true if this alias name is the 'none' alias. */ public function isNone() { return ($this->sitename == 'none') && !isset($this->env); } /** * Convert the parts of an alias name to its various component parts. * * @param string $aliasName a string representation of an alias name. */ protected function doParse($aliasName) { // Example contents of $matches: // // - a.b: // [ // 0 => 'a.b', // 1 => 'a', // 2 => '.b', // ] // // - a: // [ // 0 => 'a', // 1 => 'a', // ] if (!preg_match(self::ALIAS_NAME_REGEX, $aliasName, $matches)) { return false; } // If $matches contains only two items, then assume the alias name // contains only the environment. if (count($matches) == 2) { return $this->processSingleItem($matches[1]); } $this->sitename = $matches[1]; $this->env = ltrim($matches[2], '.'); return true; } /** * Process an alias name provided as '@sitename'. * * @param string $sitename * @return true */ protected function processSingleItem($item) { if ($this->isSpecialAliasName($item)) { $this->setSitename($item); return true; } $this->sitename = ''; $this->env = $item; return true; } /** * Determine whether the requested name is a special alias name. * * @param string $item * @return boolean */ protected function isSpecialAliasName($item) { return ($item == 'self') || ($item == 'none'); } }