2012-03-10 17:56:56 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
use PhpParser\Builder;
|
2015-03-10 16:05:55 +01:00
|
|
|
use PhpParser\Node\Stmt\Use_;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2012-03-10 17:56:56 +01:00
|
|
|
/**
|
2014-12-11 17:24:10 +01:00
|
|
|
* The following methods use reserved keywords, so their implementation is defined with an underscore and made available
|
|
|
|
* with the reserved name through __call() magic.
|
2012-03-11 09:02:52 +01:00
|
|
|
*
|
2014-12-11 17:24:10 +01:00
|
|
|
* @method Builder\Namespace_ namespace(string $name) Creates a namespace builder.
|
2014-08-11 20:53:18 +02:00
|
|
|
* @method Builder\Class_ class(string $name) Creates a class builder.
|
2014-02-06 14:44:16 +01:00
|
|
|
* @method Builder\Interface_ interface(string $name) Creates an interface builder.
|
2014-12-11 17:24:10 +01:00
|
|
|
* @method Builder\Trait_ trait(string $name) Creates a trait builder.
|
2014-12-19 18:54:56 +01:00
|
|
|
* @method Builder\Function_ function(string $name) Creates a function builder.
|
2015-03-10 16:05:55 +01:00
|
|
|
* @method Builder\Use_ use(string $name) Creates a namespace/class use builder.
|
2012-03-10 17:56:56 +01:00
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
class BuilderFactory
|
2012-03-10 17:56:56 +01:00
|
|
|
{
|
2014-12-11 17:24:10 +01:00
|
|
|
/**
|
|
|
|
* Creates a namespace builder.
|
2016-04-09 11:41:38 +02:00
|
|
|
*
|
2015-03-10 16:05:55 +01:00
|
|
|
* @param null|string|Node\Name $name Name of the namespace
|
2014-12-11 17:24:10 +01:00
|
|
|
*
|
|
|
|
* @return Builder\Namespace_ The created namespace builder
|
|
|
|
*/
|
|
|
|
protected function _namespace($name) {
|
|
|
|
return new Builder\Namespace_($name);
|
|
|
|
}
|
|
|
|
|
2012-03-11 09:02:52 +01:00
|
|
|
/**
|
|
|
|
* Creates a class builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the class
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Class_ The created class builder
|
2012-03-10 17:56:56 +01:00
|
|
|
*/
|
|
|
|
protected function _class($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Class_($name);
|
2012-03-10 17:56:56 +01:00
|
|
|
}
|
|
|
|
|
2013-05-23 10:31:59 +02:00
|
|
|
/**
|
2014-12-19 17:58:47 +01:00
|
|
|
* Creates an interface builder.
|
2013-05-23 10:31:59 +02:00
|
|
|
*
|
|
|
|
* @param string $name Name of the interface
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Interface_ The created interface builder
|
2013-05-23 10:31:59 +02:00
|
|
|
*/
|
|
|
|
protected function _interface($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Interface_($name);
|
2013-05-23 10:31:59 +02:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Creates a trait builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the trait
|
|
|
|
*
|
|
|
|
* @return Builder\Trait_ The created trait builder
|
|
|
|
*/
|
|
|
|
protected function _trait($name) {
|
|
|
|
return new Builder\Trait_($name);
|
|
|
|
}
|
|
|
|
|
2012-03-10 23:25:26 +01:00
|
|
|
/**
|
|
|
|
* Creates a method builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the method
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Method The created method builder
|
2012-03-10 23:25:26 +01:00
|
|
|
*/
|
|
|
|
public function method($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Method($name);
|
2012-03-10 23:25:26 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 00:06:02 +01:00
|
|
|
/**
|
|
|
|
* Creates a parameter builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the parameter
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Param The created parameter builder
|
2012-03-11 00:06:02 +01:00
|
|
|
*/
|
|
|
|
public function param($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Param($name);
|
2012-03-11 00:06:02 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 08:42:13 +01:00
|
|
|
/**
|
|
|
|
* Creates a property builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the property
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Property The created property builder
|
2012-03-11 08:42:13 +01:00
|
|
|
*/
|
|
|
|
public function property($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Property($name);
|
2012-03-11 08:42:13 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 09:02:52 +01:00
|
|
|
/**
|
|
|
|
* Creates a function builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the function
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Function_ The created function builder
|
2012-03-11 09:02:52 +01:00
|
|
|
*/
|
|
|
|
protected function _function($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Function_($name);
|
2012-03-11 09:02:52 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 16:05:55 +01:00
|
|
|
/**
|
|
|
|
* Creates a namespace/class use builder.
|
|
|
|
*
|
|
|
|
* @param string|Node\Name Name to alias
|
|
|
|
*
|
|
|
|
* @return Builder\Use_ The create use builder
|
|
|
|
*/
|
|
|
|
protected function _use($name) {
|
|
|
|
return new Builder\Use_($name, Use_::TYPE_NORMAL);
|
|
|
|
}
|
|
|
|
|
2012-03-10 17:56:56 +01:00
|
|
|
public function __call($name, array $args) {
|
2013-05-23 10:31:59 +02:00
|
|
|
if (method_exists($this, '_' . $name)) {
|
2017-01-19 20:55:08 +01:00
|
|
|
return $this->{'_' . $name}(...$args);
|
2012-03-10 17:56:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new \LogicException(sprintf('Method "%s" does not exist', $name));
|
2012-03-10 17:56:56 +01:00
|
|
|
}
|
2014-12-19 17:58:47 +01:00
|
|
|
}
|