|
|
<?php |
|
|
|
|
|
namespace Kanboard\Core\ExternalLink; |
|
|
|
|
|
use Kanboard\Core\Base; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExternalLinkManager extends Base |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const TYPE_AUTO = 'auto'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $providers = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $userInputType = ''; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $userInputText = ''; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function register(ExternalLinkProviderInterface $provider) |
|
|
{ |
|
|
array_unshift($this->providers, $provider); |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getProvider($type) |
|
|
{ |
|
|
foreach ($this->providers as $provider) { |
|
|
if ($provider->getType() === $type) { |
|
|
return $provider; |
|
|
} |
|
|
} |
|
|
|
|
|
throw new ExternalLinkProviderNotFound('Unable to find link provider: '.$type); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getTypes() |
|
|
{ |
|
|
$types = array(); |
|
|
|
|
|
foreach ($this->providers as $provider) { |
|
|
$types[$provider->getType()] = $provider->getName(); |
|
|
} |
|
|
|
|
|
asort($types); |
|
|
|
|
|
return array(self::TYPE_AUTO => t('Auto')) + $types; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getDependencyLabel($type, $dependency) |
|
|
{ |
|
|
$provider = $this->getProvider($type); |
|
|
$dependencies = $provider->getDependencies(); |
|
|
return isset($dependencies[$dependency]) ? $dependencies[$dependency] : $dependency; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function find() |
|
|
{ |
|
|
if ($this->userInputType === self::TYPE_AUTO) { |
|
|
$provider = $this->findProvider(); |
|
|
} else { |
|
|
$provider = $this->getProvider($this->userInputType); |
|
|
$provider->setUserTextInput($this->userInputText); |
|
|
|
|
|
if (! $provider->match()) { |
|
|
throw new ExternalLinkProviderNotFound('Unable to parse URL with selected provider'); |
|
|
} |
|
|
} |
|
|
|
|
|
if ($provider === null) { |
|
|
throw new ExternalLinkProviderNotFound('Unable to find link information from provided information'); |
|
|
} |
|
|
|
|
|
return $provider; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setUserInput(array $values) |
|
|
{ |
|
|
$this->userInputType = empty($values['type']) ? self::TYPE_AUTO : $values['type']; |
|
|
$this->userInputText = empty($values['text']) ? '' : trim($values['text']); |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setUserInputType($userInputType) |
|
|
{ |
|
|
$this->userInputType = $userInputType; |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setUserInputText($userInputText) |
|
|
{ |
|
|
$this->userInputText = $userInputText; |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function findProvider() |
|
|
{ |
|
|
foreach ($this->providers as $provider) { |
|
|
$provider->setUserTextInput($this->userInputText); |
|
|
|
|
|
if ($provider->match()) { |
|
|
return $provider; |
|
|
} |
|
|
} |
|
|
|
|
|
return null; |
|
|
} |
|
|
} |
|
|
|