Skip to content

Instantly share code, notes, and snippets.

@datio
Created March 2, 2017 06:44
Show Gist options
  • Select an option

  • Save datio/eafb33dffdeea2823371247087c6d5d6 to your computer and use it in GitHub Desktop.

Select an option

Save datio/eafb33dffdeea2823371247087c6d5d6 to your computer and use it in GitHub Desktop.

Revisions

  1. datio created this gist Mar 2, 2017.
    119 changes: 119 additions & 0 deletions xf2-class-extensions.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    package main

    import (
    "fmt"
    "io/ioutil"
    "os"
    "path/filepath"
    "regexp"
    "sort"
    "strings"
    "unicode"
    )

    // todo: Move to a separate file for readability.
    const classExtensionContents = `<?php
    namespace {addOnNamespace}\{originalNamespace};
    class {className} extends XFCP_{className}
    {
    {methods}
    }
    // ******************** FOR IDE AUTO COMPLETE ********************
    if (false)
    {
    class XFCP_{className} extends \{originalNamespace}\{className} {}
    }`

    // todo: Set values though the CLI or a config file.
    const (
    sourceDir = "/var/xf2/src/"
    destinationDir = "/var/xf2/src/addons/datio/TestAddOn/"

    addOnNamespace = "Datio/TestAddOn"
    // originalNamespace = "XF\ConnectedAccount\Storage"
    )

    var matches = []string{}

    func main() {
    // todo: Move to a another file for readability.
    _ = fmt.Sprintln(sourceDir, destinationDir, addOnNamespace, classExtensionContents)

    if err := filepath.Walk(sourceDir+"XF/", getFireableDefinitions); err != nil {
    fmt.Println(err.Error())
    return
    }

    sort.Sort(sort.StringSlice(matches))

    for index, match := range matches {
    if index > 0 && match == matches[index-1] {
    continue
    }

    // Generate here struct to be inserted into the type collection

    fmt.Println(match)
    }
    }

    var definitionRe = regexp.MustCompile(`(?:\$this->|\\XF::)extendClass\(['"]\\?(.*?)['"]\);|stringToClass\(.*?['"]\\?(.*?)['"].*?\);`)
    var (
    adminType = strings.NewReplacer(`%s\%s`, `%s\Admin`)
    cliType = strings.NewReplacer(`%s\%s`, `%s\Cli`)
    pubType = strings.NewReplacer(`%s\%s`, `%s\Pub`)
    )

    func getFireableDefinitions(path string, info os.FileInfo, err error) error {
    if info.IsDir() {
    return nil
    }

    // fileContents, err := ioutil.ReadFile(sourceDir + "XF/App.php")
    fileContents, err := ioutil.ReadFile(path)
    if err != nil {
    return err
    }

    results := definitionRe.FindAllStringSubmatch(string(fileContents), -1)

    if len(results) == 0 {
    return nil
    }

    for _, submatch := range results {
    if len(submatch[1]) > 0 {
    matches = append(matches, submatch[1])
    continue
    } else if len(submatch[2]) == 0 {
    continue
    }

    var first rune
    for _, c := range submatch[2] {
    first = c
    break
    }

    if unicode.IsLower(first) {
    continue
    }

    if strings.HasPrefix(submatch[2], `%s\%s`) {
    matches = append(matches, adminType.Replace(submatch[2]))
    matches = append(matches, cliType.Replace(submatch[2]))
    matches = append(matches, pubType.Replace(submatch[2]))
    continue
    }

    matches = append(matches, submatch[2])
    }

    return err
    }

    // todo:...
    // func findWildcardPath