Skip to content

Run getParams after init#43

Merged
eldadfux merged 2 commits intomasterfrom
fix-get-args-after-init
Jan 4, 2022
Merged

Run getParams after init#43
eldadfux merged 2 commits intomasterfrom
fix-get-args-after-init

Conversation

@Meldiron
Copy link
Contributor

@Meldiron Meldiron commented Jan 2, 2022

This allows running code that required ->match() before Utopia gets parameters. This is important, because you cannot write such a code before running ->run(), because at the top of run we do some sorting of $routes, and for that to work properly, we need to run ->match() after running this sorting.

Opened questions

Not sure how to re-write tests. A lot of tests send empty args array, some send specific args that we test. I also removed the default value that can cause some troubles.

Comment on lines +12 to +50
private static $params = null;

/**
* Get Param
*
* Get param by current method name
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getParam(string $key, $default = null): mixed
{
if($this::_hasParams() && \in_array($key, $this::_getParams())) {
return $this::_getParams()[$key];
}

return parent::getParam($key, $default);
}

/**
* Get Params
*
* Get all params of current method
*
* @return array
*/
public function getParams(): array
{
$paramsArray = [];

if($this::_hasParams()) {
$paramsArray = $this::_getParams();
}

return \array_merge($paramsArray, parent::getParams());
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are extending the request class only to add a setParams function, we only need to override the setParams method. Is there a reason you have implemented all the other methods as well ?

@eldadfux eldadfux merged commit 1c28ba9 into master Jan 4, 2022
@TorstenDittmann TorstenDittmann deleted the fix-get-args-after-init branch January 4, 2022 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants