Sha256: 6dbce2bcc0389743201b3094e27c4f0991a96830875911eee0d68ef5a75e5037

Contents?: true

Size: 1.64 KB

Versions: 54

Compression:

Stored size: 1.64 KB

Contents

module Wordmove
  module Generators
    module MovefileAdapter
      def wordpress_path
        File.expand_path(Dir.pwd)
      end

      def database
        DBConfigReader.config
      end
    end

    class DBConfigReader
      def self.config
        new.config
      end

      def config
        OpenStruct.new(database_config)
      end

      def database_config
        if wp_config_exists?
          WordpressDBConfig.config
        else
          DefaultDBConfig.config
        end
      end

      def wp_config_exists?
        File.exist?(WordpressDirectory.default_path_for(:wp_config))
      end
    end

    class DefaultDBConfig
      def self.config
        {
          name: "database_name",
          user: "user",
          password: "password",
          host: "127.0.0.1"
        }
      end
    end

    class WordpressDBConfig
      def self.config
        new.config
      end

      def wp_config
        @wp_config ||= File.read(
          WordpressDirectory.default_path_for(:wp_config)
        ).encode('utf-8', invalid: :replace)
      end

      def wp_definitions
        {
          name: 'DB_NAME',
          user: 'DB_USER',
          password: 'DB_PASSWORD',
          host: 'DB_HOST'
        }
      end

      def wp_definition_regex(definition)
        /['"]#{definition}['"],\s*["'](?<value>.*)['"]/
      end

      def defaults
        DefaultDBConfig.config.clone
      end

      def config
        wp_definitions.each_with_object(defaults) do |(key, definition), result|
          wp_config.match(wp_definition_regex(definition)) do |match|
            result[key] = match[:value]
          end
        end
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
wordmove-5.2.2 lib/wordmove/generators/movefile_adapter.rb
wordmove-5.2.1 lib/wordmove/generators/movefile_adapter.rb
wordmove-5.2.0 lib/wordmove/generators/movefile_adapter.rb
wordmove-5.1.0 lib/wordmove/generators/movefile_adapter.rb
wordmove-5.0.2 lib/wordmove/generators/movefile_adapter.rb
wordmove-5.0.1 lib/wordmove/generators/movefile_adapter.rb
wordmove-5.0.0.dev lib/wordmove/generators/movefile_adapter.rb
wordmove-5.0.0 lib/wordmove/generators/movefile_adapter.rb
wordmove-4.0.2.pre4 lib/wordmove/generators/movefile_adapter.rb
wordmove-4.0.2.pre3 lib/wordmove/generators/movefile_adapter.rb
wordmove-4.0.2.pre2 lib/wordmove/generators/movefile_adapter.rb
wordmove-4.0.2.pre1 lib/wordmove/generators/movefile_adapter.rb
wordmove-4.0.1 lib/wordmove/generators/movefile_adapter.rb
wordmove-4.0.0 lib/wordmove/generators/movefile_adapter.rb
wordmove-3.2.3 lib/wordmove/generators/movefile_adapter.rb
wordmove-3.2.3pre2 lib/wordmove/generators/movefile_adapter.rb
wordmove-3.2.3pre lib/wordmove/generators/movefile_adapter.rb
wordmove-3.2.2 lib/wordmove/generators/movefile_adapter.rb
wordmove-3.2.1 lib/wordmove/generators/movefile_adapter.rb
wordmove-3.2.0 lib/wordmove/generators/movefile_adapter.rb