X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fdrupal%2Fconsole%2Ftemplates%2Fmodule%2Fsrc%2FPlugin%2FRest%2FResource%2Frest.php.twig;fp=vendor%2Fdrupal%2Fconsole%2Ftemplates%2Fmodule%2Fsrc%2FPlugin%2FRest%2FResource%2Frest.php.twig;h=a5c3bbdce156a5def30e53efa413d805ae89adef;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/vendor/drupal/console/templates/module/src/Plugin/Rest/Resource/rest.php.twig b/vendor/drupal/console/templates/module/src/Plugin/Rest/Resource/rest.php.twig new file mode 100644 index 000000000..a5c3bbdce --- /dev/null +++ b/vendor/drupal/console/templates/module/src/Plugin/Rest/Resource/rest.php.twig @@ -0,0 +1,112 @@ +{% extends "base/class.php.twig" %} + +{% block file_path %} +\Drupal\{{module_name}}\Plugin\rest\resource\{{class_name}}. +{% endblock %} + +{% block namespace_class %} +namespace Drupal\{{module_name}}\Plugin\rest\resource; +{% endblock %} + +{% block use_class %} +use Drupal\Core\Session\AccountProxyInterface; +use Drupal\rest\Plugin\ResourceBase; +use Drupal\rest\ResourceResponse; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Psr\Log\LoggerInterface; +{% endblock %} + +{% block class_declaration %} +/** + * Provides a resource to get view modes by entity and bundle. + * + * @RestResource( + * id = "{{ plugin_id }}", + * label = @Translation("{{ plugin_label }}"), + * uri_paths = { + * "canonical" = "/{{ plugin_url }}" + * } + * ) + */ +class {{ class_name }} extends ResourceBase {% endblock %} + +{% block class_variables %} + /** + * A current user instance. + * + * @var \Drupal\Core\Session\AccountProxyInterface + */ + protected $currentUser; +{% endblock %} + +{% block class_construct %} + + /** + * Constructs a Drupal\rest\Plugin\ResourceBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param array $serializer_formats + * The available serialization formats. + * @param \Psr\Log\LoggerInterface $logger + * A logger instance. + * @param \Drupal\Core\Session\AccountProxyInterface $current_user + * A current user instance. + */ + public function __construct( + array $configuration, + $plugin_id, + $plugin_definition, + array $serializer_formats, + LoggerInterface $logger, + AccountProxyInterface $current_user) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); + + $this->currentUser = $current_user; + } +{% endblock %} + +{% block class_create %} + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->getParameter('serializer.formats'), + $container->get('logger.factory')->get('{{module_name}}'), + $container->get('current_user') + ); + } +{% endblock %} +{% block class_methods %} +{% for state in plugin_states %} + + /** + * Responds to {{ state }} requests. + * + * Returns a list of bundles for specified entity. + * + * @throws \Symfony\Component\HttpKernel\Exception\HttpException + * Throws exception expected. + */ + public function {{ state|lower }}() { + + // You must to implement the logic of your REST Resource here. + // Use current user after pass authentication to validate access. + if (!$this->currentUser->hasPermission('access content')) { + throw new AccessDeniedHttpException(); + } + + return new ResourceResponse("Implement REST State {{ state }}!"); + } +{% endfor %} +{% endblock %}