maiconpinto / cakephp-adminlte-theme

CakePHP AdminLTE Theme
http://maiconpinto.github.io/cakephp-adminlte-theme/
MIT License
174 stars 111 forks source link

Trying CakePHP4.0 #91

Closed yKanazawa closed 4 years ago

yKanazawa commented 4 years ago

CakePHP4.0 has been released so I tried it.

composer.json

{
    "name": "cakephp/app",
    "description": "CakePHP skeleton app",
    "homepage": "https://cakephp.org",
    "type": "project",
    "license": "MIT",
    "require": {
        "php": ">=7.2",
        "cakephp/cakephp": "^4.0",
        "cakephp/plugin-installer": "^1.0",
        "maiconpinto/cakephp-adminlte-theme": "*"
    },
    "require-dev": {
        "cakephp/bake": "*",
        "cakephp/cakephp-codesniffer": "^4.0",
        "cakephp/debug_kit": "^4.0",
        "josegonzalez/dotenv": "3.*",
        "phpunit/phpunit": "^8.0",
        "psy/psysh": "@stable"
    },
    "suggest": {
        "markstory/asset_compress": "An asset compression plugin which provides file concatenation and a flexible filter system for preprocessing and minification.",
        "dereuromark/cakephp-ide-helper": "After baking your code, this keeps your annotations in sync with the code evolving from there on for maximum IDE and PHPStan/Psalm compatibility."
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Test\\": "tests/",
            "Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
        }
    },
    "scripts": {
        "post-install-cmd": "App\\Console\\Installer::postInstall",
        "post-create-project-cmd": "App\\Console\\Installer::postInstall",
        "post-autoload-dump": "Cake\\Composer\\Installer\\PluginInstaller::postAutoloadDump",
        "check": [
            "@test",
            "@cs-check"
        ],
        "cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
        "cs-fix": "phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
        "stan": "phpstan analyse src/",
        "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan-shim:^0.11 && mv composer.backup composer.json",
        "test": "phpunit --colors=always"
    },
    "prefer-stable": true,
    "config": {
        "sort-packages": true
    }
}

Result (Failed)

$ php composer.phar update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - maiconpinto/cakephp-adminlte-theme 1.1.0 requires cakephp/cakephp ~3.0 -> satisfiable by cakephp/cakephp[3.0.0, ..., 3.8.7, 3.x-dev] but these conflict with your requirements or minimum-stability.
...
    - maiconpinto/cakephp-adminlte-theme 1.0.0 requires cakephp/cakephp ~3.0 -> satisfiable by cakephp/cakephp[3.0.0, ..., 3.8.7, 3.x-dev] but these conflict with your requirements or minimum-stability.
    - Installation request for maiconpinto/cakephp-adminlte-theme * -> satisfiable by maiconpinto/cakephp-adminlte-theme[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6, 1.0.7, 1.0.8, 1.1.0].

$
hareshpatel1990 commented 4 years ago

Having same issue. Please let me know that how can I upgrade plugin code to latest cakephp version.

yKanazawa commented 4 years ago
  1. Update composer.json for CakePHP4
    {
    "name": "maiconpinto/cakephp-adminlte-theme",
    "description": "CakePHP 4.x AdminLTE Theme.",
    "type": "cakephp-plugin",
    "keywords": ["cakephp", "templates", "plugin"],
    "homepage": "https://github.com/maiconpinto/cakephp-adminlte-theme",
    "license": "MIT",
    "authors": [
    {
      "name": "Maicon Pinto",
      "email": "maiconsilva.pinto@gmail.com",
      "homepage": "https://www.maiconpinto.com.br/",
      "role": "Author"
    }
    ],
    "require": {
    "php": ">=7.2",
    "cakephp/cakephp": "^4.0"
    },
    "require-dev": {
    "phpunit/phpunit": "~8.0"
    },
    "autoload": {
    "psr-4": {
      "AdminLTE\\": "src"
    }
    },
    "support": {
    "issues": "https://github.com/maiconpinto/cakephp-adminlte-theme/issues",
    "source": "https://github.com/maiconpinto/cakephp-adminlte-theme"
    }
    }
yKanazawa commented 4 years ago
  1. Fix src/View/AdminLTEView.php
    
    <?php
    namespace AdminLTE\View;

use \App\View\AppView; use \Cake\Core\App; use \Cake\Utility\Inflector;

class AdminLTEView extends AppView { protected function _paths(?string $plugin = NULL, bool $cached = true): array { $prefix = $this->request->getParam('prefix') ? Inflector::camelize($this->request->getParam('prefix')) : false; $theme = $this->theme;

    $templatePaths = App::path(static::NAME_TEMPLATE);

    $pluginPaths = [];

    foreach ($templatePaths as $templateCurrent) {
        if (!empty($theme)) {
            if (!empty($plugin)) {
                for ($i = 0, $count = count($templatePaths); $i < $count; $i++) {
                    if ($prefix) {
                        $pluginPaths[] = $templatePaths[$i] . 'Plugin'.  DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . $plugin . DIRECTORY_SEPARATOR . $prefix . DIRECTORY_SEPARATOR;
                    }

                    $pluginPaths[] = $templatePaths[$i] . 'Plugin'.  DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . $plugin . DIRECTORY_SEPARATOR;
                }
            }

            if ($prefix) {
                $themePaths[] = $templateCurrent . 'Plugin'.  DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR . $prefix . DIRECTORY_SEPARATOR;
            }

            $themePaths[] = $templateCurrent . 'Plugin'.  DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR;
        }
    }

    $paths = array_merge(
        $pluginPaths,
        $themePaths,
        parent::_paths($plugin, $cached)
    );

    return $this->_paths = $paths;
}

}

yKanazawa commented 4 years ago
  1. Fix src/View/Helper/FormHelper.php
    
    <?php

namespace AdminLTE\View\Helper;

use Cake\View\Helper\FormHelper as CakeFormHelper; use Cake\Utility\Hash; use Cake\View\View; use Cake\Utility\Inflector;

class FormHelper extends CakeFormHelper {

private $templates = [
    'button' => '<button{{attrs}}>{{text}}</button>',
    'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
    'checkboxFormGroup' => '{{label}}',
    'checkboxWrapper' => '<div class="checkbox">{{label}}</div>',
    'dateWidget' => '<div class="form-group">{{label}} {{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}</div>',
    'error' => '<span class="help-block">{{content}}</span>',
    'errorList' => '<ul>{{content}}</ul>',
    'errorItem' => '<li>{{text}}</li>',
    'file' => '<input type="file" name="{{name}}"{{attrs}}>',
    'fieldset' => '<fieldset{{attrs}}>{{content}}</fieldset>',
    'formStart' => '<form{{attrs}}>',
    'formEnd' => '</form>',
    'formGroup' => '{{label}}{{input}}',
    'hiddenBlock' => '<div style="display:none;">{{content}}</div>',
    'control' => '<input type="{{type}}" name="{{name}}"{{attrs}}/>',
    'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}/>',
    'inputSubmit' => '<input type="{{type}}"{{attrs}}/>',
    'inputContainer' => '<div class="form-group input {{type}}{{required}}">{{content}}</div>',
    'inputContainerError' => '<div class="input {{type}}{{required}} has-error">{{content}}{{error}}</div>',
    'label' => '<label class="control-label" {{attrs}}>{{text}}</label>',
    'nestingLabel' => '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>',
    'legend' => '<legend>{{text}}</legend>',
    'multicheckboxTitle' => '<legend>{{text}}</legend>',
    'multicheckboxWrapper' => '<fieldset{{attrs}}>{{content}}</fieldset>',
    'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
    'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
    'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
    'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
    'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
    'radioWrapper' => '<div class="radio">{{label}}</div>',
    'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
    'submitContainer' => '<div class="box-footer {{required}}">{{content}}</div>'
];

public function __construct(View $View, array $config = [])
{
    $this->_defaultConfig['templates'] = array_merge($this->_defaultConfig['templates'], $this->templates);
    parent::__construct($View, $config);
}

public function create($context = null, array $options = []) : string
{
    $options += ['role' => 'form'];
    return parent::create($context, $options);
}

public function button(string $title, array $options = []): string
{
    $options += ['escape' => false, 'secure' => false, 'class' => 'btn btn-success'];
    $options['text'] = $title;
    return $this->widget('button', $options);
}

public function submit(?string $caption = NULL, array $options = []): string
{
    $options += ['class' => 'btn btn-success'];
    return parent::submit($caption, $options);
}

/**
 * 
 * {@inheritDoc}
 * @see \Cake\View\Helper\FormHelper::input()
 * @deprecated 1.1.1 Use FormHelper::control() instead, due to \Cake\View\Helper\FormHelper::input() deprecation 
 */
public function input($fieldName, array $options = [])
{

    $_options = [];

    if (!isset($options['type'])) {
        $options['type'] = $this->_inputType($fieldName, $options);
    }

    switch($options['type']) {
        case 'checkbox':
        case 'radio':
        case 'date':
            break;
        default:
            $_options = ['class' => 'form-control'];
            break;

    }

    $options += $_options;

    return parent::control($fieldName, $options);
}
public function control(string $fieldName, array $options = []): string
{

    $_options = [];

    if (!isset($options['type'])) {
        $options['type'] = $this->_inputType($fieldName, $options);
    }

    switch($options['type']) {
        case 'checkbox':
        case 'radio':
        case 'date':
            break;
        default:
            $_options = ['class' => 'form-control'];
            break;

    }

    $options += $_options;

    return parent::control($fieldName, $options);
}

}

yKanazawa commented 4 years ago
  1. Migrate Template with UpgradeTool https://book.cakephp.org/4/en/appendices/4-0-upgrade-guide.html
$ cd ~/cakephp-adminlte-theme
$ ~/upgrade/bin/cake upgrade file_rename templates src
yKanazawa commented 4 years ago
  1. Migrate Locale with UpgradeTool
$ ~/upgrade/bin/cake upgrade file_rename locales src
yKanazawa commented 4 years ago

Argument must be changed (README.md)

public function beforeRender(Event $event)
↓
public function beforeRender(EventInterface $event)
yKanazawa commented 4 years ago

I'm making PR now.

yKanazawa commented 4 years ago

Fix src/View/AdminLTEView.php (Plugin -> plugin)

<?php
namespace AdminLTE\View;

use \App\View\AppView;
use \Cake\Core\App;
use \Cake\Utility\Inflector;

class AdminLTEView extends AppView
{
    protected function _paths(?string $plugin = NULL, bool $cached = true): array
    {
        $prefix = $this->request->getParam('prefix') ? Inflector::camelize($this->request->getParam('prefix')) : false;
        $theme = $this->theme;

        $templatePaths = App::path(static::NAME_TEMPLATE);

        $pluginPaths = [];

        foreach ($templatePaths as $templateCurrent) {
            if (!empty($theme)) {
                if (!empty($plugin)) {
                    for ($i = 0, $count = count($templatePaths); $i < $count; $i++) {
                        if ($prefix) {
                            $pluginPaths[] = $templatePaths[$i] . 'plugin'.  DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . $plugin . DIRECTORY_SEPARATOR . $prefix . DIRECTORY_SEPARATOR;
                        }

                        $pluginPaths[] = $templatePaths[$i] . 'plugin'.  DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . $plugin . DIRECTORY_SEPARATOR;
                    }
                }

                if ($prefix) {
                    $themePaths[] = $templateCurrent . 'plugin'.  DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR . $prefix . DIRECTORY_SEPARATOR;
                }

                $themePaths[] = $templateCurrent . 'plugin'.  DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR;
            }
        }

        $paths = array_merge(
            $pluginPaths,
            $themePaths,
            parent::_paths($plugin, $cached)
        );

        return $this->_paths = $paths;
    }
}
maiconpinto commented 4 years ago

@yKanazawa Thank you very much