github.com/goravel/framework@v1.13.9/auth/console/policy_make_command.go (about)

     1  package console
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/gookit/color"
    10  
    11  	"github.com/goravel/framework/contracts/console"
    12  	"github.com/goravel/framework/contracts/console/command"
    13  	"github.com/goravel/framework/support/file"
    14  	"github.com/goravel/framework/support/str"
    15  )
    16  
    17  type PolicyMakeCommand struct {
    18  }
    19  
    20  func NewPolicyMakeCommand() *PolicyMakeCommand {
    21  	return &PolicyMakeCommand{}
    22  }
    23  
    24  // Signature The name and signature of the console command.
    25  func (receiver *PolicyMakeCommand) Signature() string {
    26  	return "make:policy"
    27  }
    28  
    29  // Description The console command description.
    30  func (receiver *PolicyMakeCommand) Description() string {
    31  	return "Create a new policy class"
    32  }
    33  
    34  // Extend The console command extend.
    35  func (receiver *PolicyMakeCommand) Extend() command.Extend {
    36  	return command.Extend{
    37  		Category: "make",
    38  	}
    39  }
    40  
    41  // Handle Execute the console command.
    42  func (receiver *PolicyMakeCommand) Handle(ctx console.Context) error {
    43  	name := ctx.Argument(0)
    44  	if name == "" {
    45  		return errors.New("Not enough arguments (missing: name) ")
    46  	}
    47  
    48  	if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
    49  		return err
    50  	}
    51  
    52  	color.Greenln("Policy created successfully")
    53  
    54  	return nil
    55  }
    56  
    57  func (receiver *PolicyMakeCommand) getStub() string {
    58  	return PolicyStubs{}.Policy()
    59  }
    60  
    61  // populateStub Populate the place-holders in the command stub.
    62  func (receiver *PolicyMakeCommand) populateStub(stub string, name string) string {
    63  	policyName, packageName, _ := receiver.parseName(name)
    64  
    65  	stub = strings.ReplaceAll(stub, "DummyPolicy", str.Case2Camel(policyName))
    66  	stub = strings.ReplaceAll(stub, "DummyPackage", packageName)
    67  
    68  	return stub
    69  }
    70  
    71  // getPath Get the full path to the command.
    72  func (receiver *PolicyMakeCommand) getPath(name string) string {
    73  	pwd, _ := os.Getwd()
    74  
    75  	policyName, _, folderPath := receiver.parseName(name)
    76  
    77  	return filepath.Join(pwd, "app", "policies", folderPath, str.Camel2Case(policyName)+".go")
    78  }
    79  
    80  // parseName Parse the name to get the policy name, package name and folder path.
    81  func (receiver *PolicyMakeCommand) parseName(name string) (string, string, string) {
    82  	name = strings.TrimSuffix(name, ".go")
    83  
    84  	segments := strings.Split(name, "/")
    85  
    86  	policyName := segments[len(segments)-1]
    87  
    88  	packageName := "policies"
    89  	folderPath := ""
    90  
    91  	if len(segments) > 1 {
    92  		folderPath = filepath.Join(segments[:len(segments)-1]...)
    93  		packageName = segments[len(segments)-2]
    94  	}
    95  
    96  	return policyName, packageName, folderPath
    97  }